title: How to make sure pthread_cond_wait() doesn't miss any pthread_cond_signal()?
body: <p>I have a Read thread and multiple write threads as the following.</p>
  <p>Read thread:</p>
  <pre><code>pthread_mutex_lock(mutex);
  while(1){
     pthread_cond_wait(cond, mutex);
     switch(state){
     //do something
     }
  }
  pthread_mutex_unlock(mutex);
  </code></pre>
  <p>Write thread1:</p>
  <pre><code>pthread_mutex_lock(mutex);
  state = work;
  pthread_cond_signal(cond);
  //do something
  pthread_mutex_unlock(mutex);
  </code></pre>
  <p>Write thread2:</p>
  <pre><code>pthread_mutex_lock(mutex);
  state = lunch;
  pthread_cond_signal(cond);
  //do something
  pthread_mutex_unlock(mutex);
  </code></pre>
  <p>Assume <code>state</code> is the shared variable and Read thread goes into waiting before all the write threads.</p>
  <p>Say the two write threads try to get <code>mutex</code> at the same time and write1 acquires <code>mutex</code> and sends <code>cond</code>. At this point of time both write2 and read threads are locked on the <code>mutex</code>, and so far to my understanding, no thread has a priority to the <code>mutex</code> so we can't be sure when write1 releases the <code>mutex</code> that read thread will acquire it, which will lead to a missed signal and an overwritten state.</p>
  <p>I have two questions here:</p>
  <ol>
  <li>Is my understanding correct, that the above situation is possible?</li>
  <li>If so, how do I make sure that no signal is missed in my code ?</li>
  </ol>