From mboxrd@z Thu Jan 1 00:00:00 1970 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on polar.synack.me X-Spam-Level: X-Spam-Status: No, score=-0.3 required=5.0 tests=BAYES_00, REPLYTO_WITHOUT_TO_CC autolearn=no autolearn_force=no version=3.4.4 X-Google-Thread: a07f3367d7,8fa1dddecbe9cd04 X-Google-Attributes: gida07f3367d7,public,usenet X-Google-NewGroupId: yes X-Google-Language: ENGLISH,ASCII-7-bit Path: g2news2.google.com!news3.google.com!proxad.net!feeder1-2.proxad.net!newsfeed.straub-nv.de!noris.net!newsfeed.arcor.de!newsspool3.arcor-online.net!news.arcor.de.POSTED!not-for-mail From: "Dmitry A. Kazakov" Subject: Re: How to determine if task is busy or not? Newsgroups: comp.lang.ada User-Agent: 40tude_Dialog/2.0.15.1 MIME-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit Reply-To: mailbox@dmitry-kazakov.de Organization: cbb software GmbH References: <063cfc19-a8fa-41af-b948-d4f8540cb2ab@o36g2000vbl.googlegroups.com> Date: Sat, 8 Aug 2009 13:27:29 +0200 Message-ID: NNTP-Posting-Date: 08 Aug 2009 13:27:27 CEST NNTP-Posting-Host: 69b2c5ef.newsspool4.arcor-online.net X-Trace: DXC=JH;oCP`ADmdOKO]LCQ@0g`4IUK 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