title: What happens to a thread that got woken up by pthread_cond_signal() but lost competition for a mutex
body: <p>Regarding this:
  <a href="https://stackoverflow.com/questions/6206350/how-to-use-condition-variable">How To Use Condition Variable</a></p>
  
  <p>Say we have number of consumer threads that execute such code (copied from the referenced page):</p>
  
  <pre><code>while (TRUE) {
      s = pthread_mutex_lock(&amp;mtx);
      while (avail == 0) {   /* Wait for something to consume */
         s = pthread_cond_wait(&amp;cond, &amp;mtx);
      }
      while (avail &gt; 0) {   /* Consume all available units */ 
          avail--;
      }
      s = pthread_mutex_unlock(&amp;mtx);
  }
  </code></pre>
  
  <p>I assume that scenario here is: main thread calls pthread_cond_signal() to tell consumer threads to do some work.</p>
  
  <p>As I understand it - subsequent threads call pthread_mutex_lock() and then pthread_cond_wait() (which atomically unlocks the mutex). By now none of the consumer threads is claiming the mutex, they all wait on pthread_cond_wait().</p>
  
  <p>When the main thread calls pthread_cond_signal(), following the <a href="http://pubs.opengroup.org/onlinepubs/007908799/xsh/pthread_cond_signal.html" rel="nofollow noreferrer">manpage</a>, at least one thread is waken up. When any of them returns from pthread_cond_wait() it automatically claims the mutex.</p>
  
  <p>So my question is: what happens now regarding the provided example code? <em>Namely, what does the thread that lost the contest for the mutex do now?</em></p>
  
  <p>(AFAICT the thread that won the mutex, should run the rest of the code and release the mutex. The one that lost should be stuck waiting on the mutex - somewhere in the 1st <em>nested</em> <code>while</code> loop - while the winner holds it and after it's been released start blocking on pthread_cond_wait() beacuse the <code>while (avail == 0)</code> will be satisfied by then. Am I correct?)</p>