comp.lang.ada
 help / color / mirror / Atom feed
* Activating tasks at global scope
@ 1993-03-10  3:32 Scott Meyers
  1993-03-10 17:11 ` Robert I. Eachus
  1993-03-11  2:51 ` Michael Feldman
  0 siblings, 2 replies; 6+ messages in thread
From: Scott Meyers @ 1993-03-10  3:32 UTC (permalink / 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."



^ permalink raw reply	[flat|nested] 6+ messages in thread

end of thread, other threads:[~1993-03-11 17:57 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
1993-03-10  3:32 Activating tasks at global scope Scott Meyers
1993-03-10 17:11 ` 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

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