Aquarius_Girl
Aquarius_Girl

Reputation: 22946

pthread_cond_wait without a while loop

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.

Upvotes: 3

Views: 1904

Answers (1)

cmeerw
cmeerw

Reputation: 7356

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.

Upvotes: 8

Related Questions