comp.lang.ada
 help / color / mirror / Atom feed
From: "Dmitry A. Kazakov" <mailbox@dmitry-kazakov.de>
Subject: Re: How to determine if task is busy or not?
Date: Sat, 8 Aug 2009 13:27:29 +0200
Date: 2009-08-08T13:27:27+02:00	[thread overview]
Message-ID: <f3vn6yuhml4u$.10sxy6foyizyf.dlg@40tude.net> (raw)
In-Reply-To: cee04170-811b-457d-acfa-5ca385c94319@d23g2000vbm.googlegroups.com

On Sat, 8 Aug 2009 03:56:11 -0700 (PDT), Ludovic Brenta wrote:

> Tomek Walkuski wrote:
>> Is there any way to determine if task is busy or not?
>>
>> I have pool of worker tasks and I want to choose one of them to give
>> some work to do.
> 
> I don't think you should see the problem in these terms.  You don't
> have to select a task from the pool; the tasks select themselves as
> they become available for a new job, e.g. each task would look like
> 
> loop
>    Queue.Get_Next (Job);
>    Process (Job);
> end loop;
> 
> where Queue is a protected object and Get_Next removes a job from it
> and passes it to the task. Of course, if there are no jobs in the
> queue, it blocks until one arrives.

Just to add a pair words...

The scheme described by Ludovic is not only simpler, it is also race
condition free. The reverse scheme has a race condition in it. If you have
asked if a task is idle, you do not know if it still is right now (or not
terminated). To eliminate race condition you need one atomic operation
"take job or fall back" and poll the pool of task with this operation. It
is not only inefficient it is also a potential live lock or at least
unbalanced servicing policy, if you have many job suppliers.

-- 
Regards,
Dmitry A. Kazakov
http://www.dmitry-kazakov.de



  reply	other threads:[~2009-08-08 11:27 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2009-08-08 10:49 How to determine if task is busy or not? Tomek Walkuski
2009-08-08 10:56 ` Ludovic Brenta
2009-08-08 11:27   ` Dmitry A. Kazakov [this message]
2009-08-08 14:59     ` Maciej Sobczak
2009-08-08 16:00       ` Tomek Wałkuski
2009-08-08 18:22       ` Dmitry A. Kazakov
2009-08-08 18:39         ` Ludovic Brenta
replies disabled

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox