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.8 required=5.0 tests=BAYES_00,INVALID_DATE autolearn=no autolearn_force=no version=3.4.4 X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: 103376,d11f89f2e445c0a7,start X-Google-Attributes: gid103376,public X-Google-ArrivalTime: 1993-03-09 20:31:54 PST Newsgroups: comp.lang.ada Path: sparky!uunet!brunix!brunix!sdm From: sdm@cs.brown.edu (Scott Meyers) Subject: Activating tasks at global scope Message-ID: <1993Mar10.033256.24718@cs.brown.edu> Sender: news@cs.brown.edu Organization: Brown University Department of Computer Science Date: Wed, 10 Mar 1993 03:32:56 GMT Date: 1993-03-10T03:32:56+00:00 List-Id: 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."