comp.lang.ada
 help / color / mirror / Atom feed
From: "Matthew Heaney" <mheaney@on2.com>
Subject: Re: visibility of protected objects
Date: Mon, 3 Dec 2001 12:40:48 -0500
Date: 2001-12-03T12:40:48-05:00	[thread overview]
Message-ID: <u0ne1qtjhdc5fc@corp.supernews.com> (raw)
In-Reply-To: 9ue5hp$ipc$1@plutonium.btinternet.com


"Tony Gair" <tonygair@donot.spam.btinternet.com> wrote in message
news:9ue5hp$ipc$1@plutonium.btinternet.com...
>
> Does anyone how to use a protected task for inter task communication and
if
> so can they tell me..........

The example below uses a protected object to exchange a string between a
producer (who reads it from standard input) and a consumer (who writes it to
standard output).

--STX
with Ada.Strings.Unbounded;  use Ada.Strings.Unbounded;
with Ada.Text_IO;
procedure Test_ITC is

   protected Shared is
      entry Put (S : in String);
      entry Get (S : out Unbounded_String);
   private
      Have_S : Boolean := False;
      S : Unbounded_String;
   end;

   protected body Shared is

      entry Put (S : in String) when not Have_S is
      begin
         Shared.S := To_Unbounded_String (S);
         Have_S := True;
      end;

      entry Get (S : out Unbounded_String) when Have_S is
      begin
         S := Shared.S;
         Have_S := False;
      end;

   end Shared;

   protected Text_IO_Lock is
      entry Seize;
      procedure Release;
   private
      In_Use : Boolean := False;
   end;

   protected body Text_IO_Lock is

      entry Seize when not In_Use is
      begin
         In_Use := True;
      end;

      procedure Release is
      begin
         In_Use := False;
      end;

   end Text_IO_Lock;

   task Consumer;

   task body Consumer is
      S : Unbounded_String;
   begin
      loop
         Shared.Get (S);

         Text_IO_Lock.Seize;
         Ada.Text_IO.Put ("consumer got string '");
         Ada.Text_IO.Put (To_String (S));
         Ada.Text_IO.Put_Line ("'");
         Text_IO_Lock.Release;

         exit when Length (S) = 0;
      end loop;
   end Consumer;

   Line : String (1 .. 81);
   Last : Natural;

begin

   loop
      Text_IO_Lock.Seize;
      Ada.Text_IO.Put ("ready: ");
      Text_IO_Lock.Release;

      Ada.Text_IO.Get_Line (Line, Last);

      Text_IO_Lock.Seize;
      Ada.Text_IO.Put_Line ("producing string");
      Text_IO_Lock.Release;

      Shared.Put (Line (1 .. Last));

      exit when Last = 0;
   end loop;

end Test_ITC;







      parent reply	other threads:[~2001-12-03 17:40 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2001-12-03  5:04 visibility of protected objects Tony Gair
2001-12-03  3:42 ` Jeffrey Carter
2001-12-03 17:40 ` Matthew Heaney [this message]
replies disabled

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