Re: Ada tasking question



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

.



Relevant Pages

  • Re: Problem using BackGroundWorker to ping multiple LAN hosts
    ... The "loop" is realy a timer object, but yes it's not the best option (I was ... 'This issues a ping in another thread. ... Dim Pinger As New Ping ... 'Calls the aupplied worker with the current value ...
    (microsoft.public.dotnet.languages.vb)
  • Re: Structure of the multitasking server
    ... Which is a bad pattern because it propagates task termination requests to ... task Server is ... Workers: array of Worker; ... for Times in 1..7 loop ...
    (comp.lang.ada)
  • Re: Structure of the multitasking server
    ... Imagine a server with fixed number of worker tasks. ... Request: Job; ...
    (comp.lang.ada)
  • Re: BackgrounDRb Theory
    ... of immortal workers that just loop and pull jobs from a queue. ... But I'm not sure what effect having an worker running for each ... Try it first with just one worker that gets started on bdrb server start and loops, works on jobs in the queue, then sleeps and does it again. ...
    (comp.lang.ruby)
  • Re: Looking for lots of words in lots of files
    ... Seriously tho, aside from using a real indexer, I would build a set of the words I'm looking for, and then loop over each file, looping over the words and doing quick checks for containment in the set. ... I don't think that would be a complicated solution and it shouldn't be terrible at performance. ...
    (comp.lang.python)