简体   繁体   English

调用函数时,unique_lock是否已解锁?

[英]Is unique_lock unlocked when a function is called?

Let's say I have a situation like this: 假设我有这样的情况:

void consumer(){
   unique_lock<mutex> lock(mtx);
   foo();  
}

void foo(){
    /* does the thread still own the mutex here? */
}

I expect it does but I'm not 100% sure. 我希望可以,但是我不确定100%。

The destructor of unique_lock calls mtx.unlock() . unique_lock的析构unique_lock调用mtx.unlock() The destructor is called at the end of the lifetime of the lock. 析构函数在锁的生存期结束时被调用。 Generally (see comments), the end of the lifetime of the lock is : 通常(请参见注释),锁的生存期结束为:

void consumer(){
   unique_lock<mutex> lock(mtx);
   foo();  
} // <- here.

So yes, it'll still be locked. 是的,它仍然会被锁定。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 当调用condition_variable :: wait()时,未锁定的unique_lock会发生什么情况? - what happen to an unlocked unique_lock when condition_variable::wait() is called? 如果我们已经手动解锁过,unique_lock会在销毁时解锁吗? - Is unique_lock unlocked on destruction if we already unlocked it by hand? 使用unique_lock锁定互斥锁时调用std :: system_error - std::system_error called when locking mutex with unique_lock 当单个线程获取同一个互斥锁的 2 个 unique_lock 时,unique_lock 是什么意思? - what does unique_lock mean when a single thread acquire 2 unique_lock of the same mutex? 如果unique_lock在其生命周期内不会再次使用,是否应该在解锁后释放它? - Should a unique_lock be released after being unlocked if it won't be used again during its lifetime? 与conditional_variable一起使用时(unique)与unique_lock的开销 - (boost) overhead of a unique_lock when used with a conditional_variable 跨线程的unique_lock? - unique_lock across threads? mutex.lock与unique_lock - mutex.lock vs unique_lock 何时使用C ++ 11互斥锁,锁,unique_lock,shared_lock等 - When to use C++11 mutex, lock, unique_lock, shared_lock, etc 当将相同的互斥锁与其他地方的lock_guard一起使用时,在unique_lock互斥锁上等待/通知是否安全? - Is it safe to wait/notify on unique_lock mutexes when same mutexes are used with lock_guard on somewhere else?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM