4

I am working with condition variables and I am assuming they unlock their associated mutex on wait. Otherwise, the mutex would never be released. Yet, I can't find this information on 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 the "consumerWakeMeUp.wait(lk)" unlock the mutex? It must I assume otherwise the thread would hand on that mutext forever. But if anyone knows more the details I'd appreciate the input.

thank you.

Ulrich Eckhardt
  • 16,572
  • 3
  • 28
  • 55
gmmo
  • 2,577
  • 3
  • 30
  • 56
  • `Yet, I can't find this information on any documentation` it's literally point 1 [here](http://en.cppreference.com/w/cpp/thread/condition_variable/wait). – user657267 Jul 23 '15 at 06:10

1 Answers1

2

never mind

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

gmmo
  • 2,577
  • 3
  • 30
  • 56