title: pthread_cond_signal doesn't unblock pthread_cond_wait thread immediately
body: <p>I'm observing a behaviour which doesn't seem to be inline with how <code>pthread_cond_signal</code> and <code>pthread_cond_wait</code> should behave (accordingly to manpages).  <code>man 3 pthread_cond_signal</code> stipulates that:</p>
  <blockquote>
  <p>The pthread_cond_signal() function shall unblock at least one of the threads that are blocked on the specified condition variable cond (if any threads are blocked on cond).</p>
  </blockquote>
  <p>This isn't precise enough and doesn't clarify if, at the same time, the thread calling <code>pthread_cond_signal</code> will yield its time back to the scheduler.</p>
  <p>Here's an example program:</p>
  <pre><code>     1  #include &lt;pthread.h&gt;
       2  #include &lt;iostream&gt;
       3  #include &lt;time.h&gt;
       4  #include &lt;unistd.h&gt;
       5
       6  int mMsg = 0;
       7  pthread_mutex_t mMsgMutex;
       8  pthread_cond_t mMsgCond;
       9  pthread_t consumerThread;
      10  pthread_t producerThread;
      11
      12  void* producer(void* data) {
      13      (void) data;
      14      while(true) {
      15          pthread_mutex_lock(&amp;mMsgMutex);
      16          std::cout &lt;&lt; &quot;1&gt; locked&quot; &lt;&lt; std::endl;
      17          mMsg += 1;
      18          std::cout &lt;&lt; &quot;1&gt; sending signal, mMsg = &quot; &lt;&lt; mMsg &lt;&lt; &quot;&quot; &lt;&lt; std::endl;
      19          pthread_cond_signal(&amp;mMsgCond);
      20          pthread_mutex_unlock(&amp;mMsgMutex);
      21      }
      22
      23      return nullptr;
      24  }
      25
      26  void* consumer(void* data) {
      27      (void) data;
      28      pthread_mutex_lock(&amp;mMsgMutex);
      29
      30      while(true) {
      31          while (mMsg == 0) {
      32              pthread_cond_wait(&amp;mMsgCond, &amp;mMsgMutex);
      33          }
      34          std::cout &lt;&lt; &quot;2&gt; wake up, msg: &quot; &lt;&lt; mMsg &lt;&lt; std::endl;
      35          mMsg = 0;
      36      }
      37
      38      return nullptr;
      39  }
      40
      41  int main()
      42  {
      43      pthread_mutex_init(&amp;mMsgMutex, nullptr);
      44      pthread_cond_init(&amp;mMsgCond, nullptr);
      45
      46      pthread_create(&amp;consumerThread, nullptr, consumer, nullptr);
      47
      48      std::cout &lt;&lt; &quot;starting producer...&quot; &lt;&lt; std::endl;
      49
      50      sleep(1);
      51      pthread_create(&amp;producerThread, nullptr, producer, nullptr);
      52
      53      pthread_join(consumerThread, nullptr);
      54      pthread_join(producerThread, nullptr);
      55      return 0;
      56  }
  </code></pre>
  <p>Here's the output:</p>
  <pre><code>starting producer...
  1&gt; locked
  1&gt; sending signal, mMsg = 1
  1&gt; locked
  1&gt; sending signal, mMsg = 2
  1&gt; locked
  1&gt; sending signal, mMsg = 3
  1&gt; locked
  1&gt; sending signal, mMsg = 4
  1&gt; locked
  1&gt; sending signal, mMsg = 5
  1&gt; locked
  1&gt; sending signal, mMsg = 6
  1&gt; locked
  1&gt; sending signal, mMsg = 7
  1&gt; locked
  1&gt; sending signal, mMsg = 8
  1&gt; locked
  1&gt; sending signal, mMsg = 9
  1&gt; locked
  1&gt; sending signal, mMsg = 10
  2&gt; wake up, msg: 10
  1&gt; locked
  1&gt; sending signal, mMsg = 1
  1&gt; locked
  1&gt; sending signal, mMsg = 2
  1&gt; locked
  1&gt; sending signal, mMsg = 3
  ...
  </code></pre>
  <p>It seems like there's no guarantee that any <code>pthread_cond_signal</code> will indeed immediately unblock any waiting <code>pthread_cond_wait</code> thread. At the same time it seems that any amount of <code>pthread_cond_signal</code> can be lost after first one has been issued.</p>
  <p>Is this really the intended behaviour or am I doing something wrong here?</p>