title: What's the point of locking and unlocking a mutex if pthread_cond_wait does that itself?
body: <p>I'm trying to learn the difference between mutexes and conditional variables and am confused by the following code.</p>
  
  <pre><code>// Lock mutex and then wait for signal to relase mutex
  pthread_mutex_lock( &amp;count_mutex );
  
  /*Wait while functionCount2() operates on count
  mutex unlocked if condition varialbe in functionCount2() signaled. &lt;-- so then why call pthread_mutex_unlock() 3 lines latter if it's already unlocked?*/
  pthread_cond_wait( &amp;condition_var, &amp;count_mutex );
  count++;
  printf("Counter value functionCount1: %d\n",count);
  
  pthread_mutex_unlock( &amp;count_mutex );
  </code></pre>
  
  <p>According to <a href="http://pubs.opengroup.org/onlinepubs/007908775/xsh/pthread_cond_wait.html" rel="nofollow">the docs</a> <code>pthread_cond_wait()</code> "atomically release mutex and cause the calling thread to block on the condition variable" so what's the point of <code>pthread_mutex_lock( &amp;count_mutex );</code> if <code>pthread_cond_wait()</code> just unlocks it and what's the point to the latter call pthread_mutex_unlock() since <code>pthread_cond_wait()</code> had already unlocked it?</p>
  
  <p>To me it would make sense if pthread_cond_wait() did not take a mutex, and I thought that was its point. </p>