Re: Ada tasking question
- From: "jimmaureenrogers@xxxxxxxxxxxxxxxx" <jimmaureenrogers@xxxxxxxxxxxxxxxx>
- Date: 18 Apr 2007 19:13:55 -0700
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
.
- References:
- Ada tasking question
- From: Stefan Bellon
- Ada tasking question
- Prev by Date: Re: Ada tasking question
- Next by Date: Re: Ada tasking question
- Previous by thread: Re: Ada tasking question
- Next by thread: Re: Ada tasking question
- Index(es):
Relevant Pages
|