pthread_cond_wait without a while loop

condition-variable, multithreading, mutex, pthreads

Solution

A loop is compulsory because according to http://pubs.opengroup.org/onlinepubs/009695399/functions/pthread_cond_wait.html:

Spurious wakeups from the pthread_cond_timedwait() or pthread_cond_wait() functions may occur. Since the return from pthread_cond_timedwait() or pthread_cond_wait() does not imply anything about the value of this predicate, the predicate should be re-evaluated upon such return.

Problem

``` global variable 'temp'; **threadA** -pthread_mutex_lock- if (temp == 'x') -pthread_cond_wait- do this -pthread_mutex_unlock- **threadB** -pthread_mutex_lock- if (someCondition == true) temp = 'x' -pthread_cond_signal- -pthread_mutex_unlock- ``` In my case I may not have any loops, I just have an if condition. So, I want that when temp == 'x', then the threadA should do that/this. - Is the loop compulsory when dealing with the `pthread_cond_wait`? - What is the other way for writing the code if we don't need loops? - Is this a correct way of writing the code?

Original source