From owner-freebsd-hackers@FreeBSD.ORG Mon Feb 28 02:41:47 2011 Return-Path: Delivered-To: freebsd-hackers@freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2001:4f8:fff6::34]) by hub.freebsd.org (Postfix) with ESMTP id CE591106566C for ; Mon, 28 Feb 2011 02:41:47 +0000 (UTC) (envelope-from pieter@degoeje.nl) Received: from smtp.utwente.nl (smtp1.utsp.utwente.nl [130.89.2.8]) by mx1.freebsd.org (Postfix) with ESMTP id 536B08FC14 for ; Mon, 28 Feb 2011 02:41:46 +0000 (UTC) Received: from nox-laptop.student.utwente.nl (nox-laptop.student.utwente.nl [130.89.160.140]) by smtp.utwente.nl (8.12.10/SuSE Linux 0.7) with ESMTP id p1S29uGJ009484; Mon, 28 Feb 2011 03:09:56 +0100 From: Pieter de Goeje To: freebsd-hackers@freebsd.org Date: Mon, 28 Feb 2011 03:09:56 +0100 User-Agent: KMail/1.9.10 References: <4D6ABA14.80208@rawbw.com> <4D6AC17A.7020505@rawbw.com> In-Reply-To: <4D6AC17A.7020505@rawbw.com> MIME-Version: 1.0 Content-Type: text/plain; charset="iso-8859-1" Content-Transfer-Encoding: 7bit Content-Disposition: inline Message-Id: <201102280309.56631.pieter@degoeje.nl> X-UTwente-MailScanner-Information: Scanned by MailScanner. Contact icts.servicedesk@utwente.nl for more information. X-UTwente-MailScanner: Found to be clean X-UTwente-MailScanner-From: pieter@degoeje.nl X-Spam-Status: No Cc: Yuri Subject: Re: Is pthread_cond_signal(3) man page correct? X-BeenThere: freebsd-hackers@freebsd.org X-Mailman-Version: 2.1.5 Precedence: list List-Id: Technical Discussions relating to FreeBSD List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Mon, 28 Feb 2011 02:41:47 -0000 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