From: sdm@cs.brown.edu (Scott Meyers)
Subject: Activating tasks at global scope
Date: Wed, 10 Mar 1993 03:32:56 GMT
Date: 1993-03-10T03:32:56+00:00 [thread overview]
Message-ID: <1993Mar10.033256.24718@cs.brown.edu> (raw)
The code below implements a producer-consumer system containing one
producing task, two consuming tasks, and a bounded buffer task. I am only
interested in the tasking interactions, so I've omitted the actual buffer
manipulations. This code works fine under the Ada/ED compiler. However, I
would prefer to put the tasks at global scope rather than nest them inside
the producer_consumer procedure. I tried to do this by putting each task
inside a package, but then I couldn't figure out how to activate the tasks
when producer_consumer was invoked. What I'd like is an architecture where
the tasks are automatically started before or at the same time the the main
subprogram is invoked.
If I've done anything particularly gross, feel free to comment
appropriately. Nobody here knows Ada, so I put this together by copying
fairly liberally from Ben-Ari's book, "Principles of Concurrent and
Distributed Programming."
Thanks,
Scott
with Text_IO;
use Text_IO;
procedure producer_consumer is
task Buffer is
entry insert(item: in Integer);
entry remove(item: out Integer);
end Buffer;
task body Buffer is
count: Integer := 0;
begin
loop
select when count < 10 =>
accept insert(item: in Integer) do
-- insert item into the buffer
null;
end insert;
count := count + 1;
or when count > 0 =>
accept remove(item: out Integer) do
-- remove an object from the buffer and assign it to item
null;
end remove;
count := count - 1;
end select;
end loop;
end Buffer;
task Producer;
task body Producer is
n: integer;
begin
loop
-- give n an appropriate value
Buffer.insert(n);
end loop;
end Producer;
task Consumer1;
task body Consumer1 is
n: integer;
begin
loop
Buffer.remove(n);
-- do something with n
end loop;
end Consumer1;
task Consumer2;
task body Consumer2 is
n: integer;
begin
loop
Buffer.remove(n);
-- do something with n
end loop;
end Consumer2;
begin
null;
end;
-------------------------------------------------------------------------------
What do you say to a convicted felon in Providence? "Hello, Mr. Mayor."
next reply other threads:[~1993-03-10 3:32 UTC|newest]
Thread overview: 7+ messages / expand[flat|nested] mbox.gz Atom feed top
1993-03-10 3:32 Scott Meyers [this message]
1993-03-10 17:11 ` Activating tasks at global scope Robert I. Eachus
1993-03-11 3:56 ` Scott Meyers
1993-03-11 17:57 ` Dave Collard x7468
1993-03-11 2:51 ` Michael Feldman
1993-03-11 2:54 ` second half of portable diners Michael Feldman
-- strict thread matches above, loose matches on Subject: below --
1993-03-15 14:58 Activating tasks at Global Scope Cheryl Marquis
replies disabled
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox