Originally Posted by
CornedBee
The implementation will wait until it can lock the mutex. Then it will check the condition. If it's false, it will unlock the mutex and go back to sleep.
That isn't true. The mutex might be re-acquired even though the condition no longer holds. This is why all calls to pthread_cond_wait() should be wrapped in a while (not an if!) which tests the condition. Some other thread could intervene and cause the condition to become untrue even though the testing thread has woken up.
In general:
Code:
pthread_mutex_lock(&mut);
while(!some_condition)
{
pthread_cond_wait(&cond, &mut);
}
/* Do something to the state... */
pthread_mutex_unlock(&mut);