From owner-freebsd-questions@FreeBSD.ORG Tue Feb 22 12:43:53 2005 Return-Path: Delivered-To: freebsd-questions@freebsd.org Received: from mx1.FreeBSD.org (mx1.freebsd.org [216.136.204.125]) by hub.freebsd.org (Postfix) with ESMTP id C27D016A4CE for ; Tue, 22 Feb 2005 12:43:53 +0000 (GMT) Received: from kane.otenet.gr (kane.otenet.gr [195.170.0.27]) by mx1.FreeBSD.org (Postfix) with ESMTP id 2F47743D49 for ; Tue, 22 Feb 2005 12:43:48 +0000 (GMT) (envelope-from keramida@ceid.upatras.gr) Received: from orion.daedalusnetworks.priv (aris.bedc.ondsl.gr [62.103.39.226])j1MCgaqt021624; Tue, 22 Feb 2005 14:42:36 +0200 Received: from orion.daedalusnetworks.priv (orion [127.0.0.1]) j1MCgi7i080488; Tue, 22 Feb 2005 14:42:44 +0200 (EET) (envelope-from keramida@ceid.upatras.gr) Received: (from keramida@localhost)j1MCgi34080487; Tue, 22 Feb 2005 14:42:44 +0200 (EET) (envelope-from keramida@ceid.upatras.gr) Date: Tue, 22 Feb 2005 14:42:44 +0200 From: Giorgos Keramidas To: Jonathon McKitrick Message-ID: <20050222124244.GA80450@orion.daedalusnetworks.priv> References: <20050222055000.GA49525@dogma.freebsd-uk.eu.org> <20050222063810.GA1784@gothmog.gr> <20050222121911.GA63213@dogma.freebsd-uk.eu.org> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20050222121911.GA63213@dogma.freebsd-uk.eu.org> cc: freebsd-questions@freebsd.org Subject: Re: Best way to share data between threads X-BeenThere: freebsd-questions@freebsd.org X-Mailman-Version: 2.1.1 Precedence: list List-Id: User questions List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Tue, 22 Feb 2005 12:43:53 -0000 On 2005-02-22 12:19, Jonathon McKitrick wrote: >On Tue, Feb 22, 2005 at 08:38:11AM +0200, Giorgos Keramidas wrote: >:On 2005-02-22 05:50, Jonathon McKitrick wrote: >:> >:> Hi all, >:> I'm porting some libraries from Win32 to BSD/Linux. In the >:> original code, I receive a Windows event with an attached COM >:> object. >:> >:> Under *nix, what is the best way to copy this? A message, >:> followed by accessing shared memory between threads? >: >: Does the message really have to be shared across many threads? I'm >: only asking because thread-specific message queues are not that >: hard to build with pthreads. You will only need a per-thread queue >: to hold messages and a master thread that 'dispatches' messages to >: the proper thread/queue. > > Well, there will be a data acquisition thread, that when finished, > will need to signal the main processing thread that a new data > object has arrived. There are a couple of ways to do the notification: 1) Explicit notification using a condition variable. 2) No notification at all, but restructuring of the thread code to use message queues. The first can be accomplished by associating a pthread_cond_t with the data storage area and explicit signalling of the processing thread. Every time the processing thread has nothing to do (i.e. no data being processed at the time), it blocks on the condition variable and waits until the data acquisition thread awakes it. The second way to solve this is to use minimal synchronization (i.e. a mutex to protect a common message queue) and a queue of currently pending messages. The data acquisition thread gets a copy of the data and pushes it on the queue, leaving all further work to the processing thread. The processing threads awakes periodically, "polling" for new messages in the queue. When one is found, it is immediately processed. When no message is found, the processing thread sleeps for a small amount of time.