From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 15639 invoked by alias); 4 Mar 2005 23:18:35 -0000 Mailing-List: contact pthreads-win32-help@sources.redhat.com; run by ezmlm Precedence: bulk List-Subscribe: List-Archive: List-Post: List-Help: , Sender: pthreads-win32-owner@sources.redhat.com Received: (qmail 15629 invoked from network); 4 Mar 2005 23:18:31 -0000 Received: from unknown (HELO canyonero.dot.net.au) (202.147.68.14) by sourceware.org with SMTP; 4 Mar 2005 23:18:31 -0000 Received: from [202.147.74.107] (helo=ip-74-170.dot.net.au) by canyonero.dot.net.au with esmtp (Exim 3.35 #1 (Debian)) id 1D7M3y-0007Wn-00 for ; Sat, 05 Mar 2005 10:18:30 +1100 Subject: Re: starvation in pthread_once? From: Ross Johnson To: Pthreads-Win32 list In-Reply-To: <97ffb3105030301323c1bae1@mail.gmail.com> References: <97ffb3105030301323c1bae1@mail.gmail.com> Content-Type: text/plain Date: Fri, 04 Mar 2005 23:18:00 -0000 Message-Id: <1109978309.8332.70.camel@desk.home> Mime-Version: 1.0 Content-Transfer-Encoding: 7bit X-SW-Source: 2005/txt/msg00022.txt.bz2 On Thu, 2005-03-03 at 04:32 -0500, Gottlob Frege wrote: > I'm concerned about the Sleep(0) in pthread_once: > Thanks. It looks like this routine needs to be redesigned. Regards. Ross > if (!once_control->done) > { > if (InterlockedIncrement (&(once_control->started)) == 0) > { > /* > * First thread to increment the started variable > */ > (*init_routine) (); > once_control->done = PTW32_TRUE; > > } > else > { > /* > * Block until other thread finishes executing the onceRoutine > */ > while (!(once_control->done)) > { > /* > * The following gives up CPU cycles without pausing > * unnecessarily > */ > Sleep (0); > } > } > } > > IIRC, Sleep(0) does not relinquish time slices to lower priority > threads. (Sleep(n) for n != 0 does, but 0 does not). So, if a lower > priority thread is first in, followed closely by a higher priority > one, the higher priority thread will spin on Sleep(0) *forever* > because the lower, first thread will never get a chance to set done. > > So even Sleep(10) should be good enough. In theory, there could be > enough higher priority threads in the system that the first thread > still doesn't get in (ever?!), but unlikely. And that would probably > mean a general design flaw of the calling code, not pthread_once. > > ? >