Is pthread_cond_signal(3) man page correct?
Pieter de Goeje
pieter at degoeje.nl
Mon Feb 28 02:41:47 UTC 2011
On Sunday 27 February 2011 22:26:18 Yuri wrote:
> Also I want to add that I came to this question while observing behavior
> consistent with multiple wakeup on FreeBSD-8.1. The heavily
> multi-threaded code that assumes that only one thread can be woken up by
> one pthread_cond_signal call crashes, and the only reasonable
> explanation so far is that more than one threads are actually being
> woken up.
pthread_cond_signal() can indeed wake up more than one thread. That's why you
should always wrap pthread_cond_wait() in a loop. For example a blocking
queue could be implemented like this (pseudo code):
take() {
pthread_mutex_lock(mutex);
while(queue->empty()) {
pthread_cond_wait(cond, mutex);
}
item = queue->get();
pthread_mutex_unlock(mutex);
return item;
}
put(item) {
pthread_mutex_lock(mutex);
queue->put(item);
pthread_cond_signal(cond);
pthread_mutex_unlock(mutex);
}
pthread_cond_signal() itself never blocks.
Hope this helps.
--
Pieter de Goeje
More information about the freebsd-hackers
mailing list