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=-1.9 required=5.0 tests=BAYES_00 autolearn=ham autolearn_force=no version=3.4.4 X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: 103376,b804a5a1410cced7 X-Google-Attributes: gid103376,public X-Google-ArrivalTime: 2001-12-03 09:36:59 PST Path: archiver1.google.com!news1.google.com!sn-xit-02!sn-post-01!supernews.com!corp.supernews.com!not-for-mail From: "Matthew Heaney" Newsgroups: comp.lang.ada Subject: Re: visibility of protected objects Date: Mon, 3 Dec 2001 12:40:48 -0500 Organization: Posted via Supernews, http://www.supernews.com Message-ID: References: <9ue5hp$ipc$1@plutonium.btinternet.com> X-Priority: 3 X-MSMail-Priority: Normal X-Newsreader: Microsoft Outlook Express 5.50.4807.1700 X-MimeOLE: Produced By Microsoft MimeOLE V5.50.4807.1700 X-Complaints-To: newsabuse@supernews.com Xref: archiver1.google.com comp.lang.ada:17353 Date: 2001-12-03T12:40:48-05:00 List-Id: "Tony Gair" 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;