comp.lang.ada
 help / color / mirror / Atom feed
From: "jimmaureenrogers@worldnet.att.net" <jimmaureenrogers@worldnet.att.net>
Subject: Re: Ada tasking question
Date: 18 Apr 2007 19:13:55 -0700
Date: 2007-04-18T19:13:55-07:00	[thread overview]
Message-ID: <1176948835.132931.32230@q75g2000hsh.googlegroups.com> (raw)
In-Reply-To: <20070418201307.18a85fd9@cube.tz.axivion.com>

It is not efficient to create and destroy a number of tasks
to do this job. It is more efficient to create a static set of
tasks and let them cycle through the work as in the
following example:

with Ada.Text_Io; use Ada.Text_Io;

procedure Task_Test_01 is
   subtype Return_Value is Natural range 0..100;
   subtype Index is Return_Value range 1..Return_Value'Last;
   protected Indexer is
      procedure Get_Index(Next_Index : out Return_Value);
   private
      Current_Index : Return_Value := Return_Value'Last;
   end Indexer;

   protected body Indexer is
      procedure Get_Index(Next_Index : out Return_Value) is
      begin
         Next_Index := Current_Index;
         if Next_Index > Return_Value'First then
            Current_Index := Current_Index - 1;
         end if;
      end Get_Index;
   end Indexer;

   type Buckets is array(Index) of Integer;
   The_Buckets : Buckets := (Others => 0);

   task type Worker is
      entry Set_Id(Id : Positive);
   end Worker;

   task body Worker is
      My_Index : Return_Value;
      Working_Value : Integer;
      Id_Num : Positive;
   begin
      accept Set_Id(Id : Positive) do
         Id_Num := Id;
      end Set_Id;

      loop
         Indexer.Get_Index(My_Index);
         exit when My_Index = Return_Value'First;
         Working_Value := 2 * My_Index;
         Put_Line("Worker" & Positive'Image(Id_Num) & " at work.");
         The_Buckets(My_Index) := Working_Value;
      end loop;
   end Worker;
   Team : array(1..5) of Worker;
begin
   for I in Team'range loop
      Team(I).Set_Id(I);
   end loop;
end Task_Test_01;

Jim Rogers




  parent reply	other threads:[~2007-04-19  2:13 UTC|newest]

Thread overview: 20+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2007-04-18 18:13 Ada tasking question Stefan Bellon
2007-04-18 18:40 ` Randy Brukardt
2007-04-18 20:12   ` Jeffrey R. Carter
2007-04-18 22:43     ` Stefan Bellon
2007-04-19  2:02       ` Steve
2007-04-18 21:08   ` Leif Holmgren
2007-04-18 22:57     ` Stefan Bellon
2007-04-18 23:41       ` Brian May
2007-04-19  0:25       ` Randy Brukardt
2007-04-19  8:02         ` Stefan Bellon
2007-04-19 12:47         ` Jacob Sparre Andersen
2007-04-19 16:11           ` Anh Vo
2007-04-20  4:32           ` Jeffrey R. Carter
2007-04-19  0:50       ` Jeffrey R. Carter
2007-04-18 19:50 ` Alex R. Mosteo
2007-04-18 23:00   ` Stefan Bellon
2007-04-19 20:37   ` Pascal Obry
2007-04-19  2:13 ` jimmaureenrogers [this message]
2007-04-19  7:49 ` Dmitry A. Kazakov
  -- strict thread matches above, loose matches on Subject: below --
1996-10-18  0:00 whiting_ms@corning.com (Matt Whiting)
replies disabled

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