tech-userlevel archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

Re: pthread_cond_signal() ambiguity



On Wednesday 02 December 2009 22:11:00 Luke Mewburn wrote:

> AFAICT, the NetBSD manual page and/or the implementation are wrong
> in this respect.
> 
> Based on my investigation on this topic recently, it is valid to call
> pthread_cond_signal() (and _broadcast()) without the mutex held,
> according to the POSIX standard and other implementations I've also
> used (Linux 2.6, Darwin).
> You'll get an unpredictable wakeup of the sleeping thread, and/or
> possibly a spurious wakeup, so you need to retest the condition
> after being awoken, but that should be it.

That's what Butenhof says in his "Programming with Posix threads" book. 
However some people seem to disagree with him and Posix standard.

As far as I understand this would be the correct code for a thread to wait on 
a condition variable:

pthread_mutex_lock(&mutex);

while(predicate == false)
        pthread_cond_wait(&cond, &mutex);

if(predicate == true)
        do_stuff();
        ...
        pthread_mutex_unlock(&mutex);

So if another thread is calling pthread_cond_signal() without having a mutex 
locked, there must be some hidden mutual exclusion going on in the pthreads 
library on that condition variable, in order to allow this to happen in a safe 
way. Does anyone know low level details of how NetBSD implements these 
functions?


Home | Main Index | Thread Index | Old Index