Date: Mon, 28 Feb 2011 03:09:56 +0100 From: Pieter de Goeje <pieter@degoeje.nl> To: freebsd-hackers@freebsd.org Cc: Yuri <yuri@rawbw.com> Subject: Re: Is pthread_cond_signal(3) man page correct? Message-ID: <201102280309.56631.pieter@degoeje.nl> In-Reply-To: <4D6AC17A.7020505@rawbw.com> References: <4D6ABA14.80208@rawbw.com> <4D6AC17A.7020505@rawbw.com>
next in thread | previous in thread | raw e-mail | index | archive | help
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
Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?201102280309.56631.pieter>