条件变量会解锁它们的互斥量吗?
do condition variables unlock their mutex?
我正在使用条件变量,我假设它们在等待时解锁相关联的互斥锁。否则,互斥锁永远不会被释放。但是,我无法在任何文档中找到此信息。考虑以下代码:
std::condition_variable consumerWakeMeUp;
std::mutex queueMutex;
// this locks the mutex
std::unique_lock<std::mutex> lk(queueMutex);
// going to sleep now
consumerWakeMeUp.wait(lk);
"consumerWakeMeUp.wait(lk)" 是否解锁了互斥体?我必须假设否则线程将永远传递该互斥体。但如果有人知道更多细节,我将不胜感激。
谢谢。
没关系
"Atomically releases lock, blocks the current executing thread, and adds it to the list of threads waiting on *this. The thread will be unblocked when notify_all() or notify_one() is executed. It may also be unblocked spuriously. When unblocked, regardless of the reason, lock is reacquired and wait exits. If this function exits via exception, lock is also reacquired. (until C++14)"
我正在使用条件变量,我假设它们在等待时解锁相关联的互斥锁。否则,互斥锁永远不会被释放。但是,我无法在任何文档中找到此信息。考虑以下代码:
std::condition_variable consumerWakeMeUp;
std::mutex queueMutex;
// this locks the mutex
std::unique_lock<std::mutex> lk(queueMutex);
// going to sleep now
consumerWakeMeUp.wait(lk);
"consumerWakeMeUp.wait(lk)" 是否解锁了互斥体?我必须假设否则线程将永远传递该互斥体。但如果有人知道更多细节,我将不胜感激。
谢谢。
没关系
"Atomically releases lock, blocks the current executing thread, and adds it to the list of threads waiting on *this. The thread will be unblocked when notify_all() or notify_one() is executed. It may also be unblocked spuriously. When unblocked, regardless of the reason, lock is reacquired and wait exits. If this function exits via exception, lock is also reacquired. (until C++14)"