From owner-freebsd-current Thu Aug 6 15:39:08 1998 Return-Path: Received: (from majordom@localhost) by hub.freebsd.org (8.8.8/8.8.8) id PAA24025 for freebsd-current-outgoing; Thu, 6 Aug 1998 15:39:08 -0700 (PDT) (envelope-from owner-freebsd-current@FreeBSD.ORG) Received: from cimlogic.com.au (cimlog.lnk.telstra.net [139.130.51.31]) by hub.freebsd.org (8.8.8/8.8.8) with ESMTP id PAA24006 for ; Thu, 6 Aug 1998 15:39:02 -0700 (PDT) (envelope-from jb@cimlogic.com.au) Received: (from jb@localhost) by cimlogic.com.au (8.8.8/8.8.7) id IAA02167; Fri, 7 Aug 1998 08:44:09 +1000 (EST) (envelope-from jb) From: John Birrell Message-Id: <199808062244.IAA02167@cimlogic.com.au> Subject: Re: Pthreads woes revisited. In-Reply-To: <19980806130122.38511@kublai.com> from Brian Cully at "Aug 6, 98 01:01:22 pm" To: shmit@kublai.com Date: Fri, 7 Aug 1998 08:44:08 +1000 (EST) Cc: current@FreeBSD.ORG X-Mailer: ELM [version 2.4ME+ PL40 (25)] MIME-Version: 1.0 Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: 7bit Sender: owner-freebsd-current@FreeBSD.ORG Precedence: bulk X-Loop: FreeBSD.ORG Brian Cully wrote: > I've put together a small C program that demonstrates the problems > I described in my last message. You can find the program attached > to this message. > > Note the location of the `sleep(1)' call, it's important. If it moves > outside of the mutex lock/unlock bits, everything functions normally, > but if it gets called after the lock, but before the unlock, the first > signal is missed. Mutexes can be implemented with spinlocks so that a thread that is blocked trying to lock a mutex will burn CPU and only when it exceeds it's time slice will it be interrupted to allow another thread to run. For this reason, mutexes should only be used to gain exclusive access to something and a condition variable should be used to wait. The mutex associated with the condition variable _must_ be locked prior to calling the cond_wait function. The cond_wait function will queue the calling thread and unlock the mutex. When the condition variable is signalled, a waiting thread is granted access by locking the mutex just before the cond_wait function returns. The woken thread should do what it needs to do quickly, then unlock the mutex or wait again (causing the mutex to be unlocked). More than one thread may be woken, but only one will get access to the mutex - all the rest will burn CPU, so each thread must unlock the mutex as quickly as possible. Signalling a condition variable only serves to wake any thread that might be waiting. If a thread hasn't called a cond_wait function, it won't be in a position to be woken by the signal. The thread should not be coded to use the occurrence of a signal as the driving event. Instead there should be a variable that is protected by the mutex that is changed to be used as the event data. The thread that is being driven by this data then locks the mutex, checks the variable and if there is no event data there, it calls the cond_wait. If there _is_ data already there (as the result of another thread getting there before it), no wait is necessary and the thread just unlocks the mutex and goes about it's business. If you need to buffer more than one event, then the event variable needs to be an array or a list. Whatever the variable is, it must only be accessed (both read and write) with the mutex locked. -- John Birrell - jb@cimlogic.com.au; jb@freebsd.org http://www.cimlogic.com.au/ CIMlogic Pty Ltd, GPO Box 117A, Melbourne Vic 3001, Australia +61 418 353 137 To Unsubscribe: send mail to majordomo@FreeBSD.org with "unsubscribe freebsd-current" in the body of the message