title: Does a mutex get unlocked when calling notify on a condition variable?
body: <p>I am trying to understand what happens to a mutex when it is used in a condition variable.</p>
  
  <p>In the following example, taken from <a href="http://en.cppreference.com/w/cpp/thread/condition_variable" rel="nofollow noreferrer">cppreference</a></p>
  
  <pre><code>int main()
  {
      std::queue&lt;int&gt; produced_nums;
      std::mutex m;
      std::condition_variable cond_var;
      bool done = false;
      bool notified = false;
  
      std::thread producer([&amp;]() {
          for (int i = 0; i &lt; 5; ++i) {
              std::this_thread::sleep_for(std::chrono::seconds(1));
              std::unique_lock&lt;std::mutex&gt; lock(m);
              std::cout &lt;&lt; "producing " &lt;&lt; i &lt;&lt; '\n';
              produced_nums.push(i);
              notified = true;
              cond_var.notify_one();
          }   
  
          done = true;
          cond_var.notify_one();
      }); 
  
      std::thread consumer([&amp;]() {
          std::unique_lock&lt;std::mutex&gt; lock(m);
          while (!done) {
              while (!notified) {  // loop to avoid spurious wakeups
                  cond_var.wait(lock);
              }   
              while (!produced_nums.empty()) {
                  std::cout &lt;&lt; "consuming " &lt;&lt; produced_nums.front() &lt;&lt; '\n';
                  produced_nums.pop();
              }   
              notified = false;
          }   
      }); 
  
      producer.join();
      consumer.join();
  }
  </code></pre>
  
  <p>The producer thread calls <code>cond_var.notify_one()</code> before the mutex gets unlocked. Does the mutex <code>m</code> get unlocked when notify is called, or does the notification occurs only when the mutex gets unlocked?</p>