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 _______________________________________________ freebsd-hackers@freebsd.org mailing list http://lists.freebsd.org/mailman/listinfo/freebsd-hackers To unsubscribe, send any mail to "freebsd-hackers-unsubscr...@freebsd.org"