title: Does pthread_cond_wait and pthread_mutex_unlock conflict?
body: <p>I am implementing manual reset event using pthread in Linux which is similar to WaitForSingleEvent in Windows. I found this post </p>
  
  <p><a href="https://stackoverflow.com/questions/178114/pthread-like-windows-manual-reset-event">pthread-like windows manual-reset event</a></p>
  
  <p>and follow it, however there a thing that confuse me:</p>
  
  <pre><code>void mrevent_wait(struct mrevent *ev) {
       pthread_mutex_lock(&amp;ev-&gt;mutex);
       while (!ev-&gt;triggered)
           pthread_cond_wait(&amp;ev-&gt;cond, &amp;ev-&gt;mutex);
       pthread_mutex_unlock(&amp;ev-&gt;mutex);
  }
  </code></pre>
  
  <ul>
  <li>pthread_cond_wait: 
  Atomically release mutex and cause the calling thread to block on the condition variable cond; </li>
  <li>pthread_mutex_unlock:
  Attempts to unlock the specified mutex. If the mutex type is PTHREAD_MUTEX_NORMAL, error detection is not provided. If a thread attempts to unlock a mutex that is <strong>has not locked</strong> or a mutex which is unlocked, undefined behavior results.</li>
  </ul>
  
  <p>What I am scare is when pthread_cond_wait release the mutex, <strong>then pthread_mutex_unlock may come undefined behavior</strong> (this kind of thing would drive me crazy, how come they not handle it :-D )</p>
  
  <p>Thank you.</p>