[英]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.