Do variable conditions unlock their mutex?

I am working with state variables and I am assuming that they will unblock the associated mutexes while waiting. Otherwise, the mutex will never be released. However, I cannot find this information in any documentation. Consider the following code:

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);

      

Does "consumerWakeMeUp.wait (lk)" unlock the mutex? Should I assume that the thread will pass this mutext forever. But if anyone knows more details I would appreciate some input.

thank.

+3


source to share


1 answer


Do not think



"Atomically releases the lock, locks the currently executing thread, and adds it to the list of threads waiting * for it. The thread will be unlocked if notify_all () or notify_one () is executed, and may also be falsely unlocked. When unlocked, whatever the reason, the lock will be restored and will wait to exit. If this function exits due to an exception, the lock will also be reloaded. (before C ++ 14) "

+1


source







All Articles