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-Thread: 103376,126ce244c524526b X-Google-Attributes: gid103376,public,usenet X-Google-Language: ENGLISH,ASCII-7-bit Path: g2news2.google.com!postnews.google.com!e9g2000prf.googlegroups.com!not-for-mail From: "jimmaureenrogers@worldnet.att.net" Newsgroups: comp.lang.ada Subject: Re: Tasking issues Date: Sat, 11 Aug 2007 11:51:11 -0700 Organization: http://groups.google.com Message-ID: <1186858271.483486.112630@e9g2000prf.googlegroups.com> References: <1186851804.567302.223160@q4g2000prc.googlegroups.com> NNTP-Posting-Host: 75.70.221.169 Mime-Version: 1.0 Content-Type: text/plain; charset="us-ascii" X-Trace: posting.google.com 1186858272 26624 127.0.0.1 (11 Aug 2007 18:51:12 GMT) X-Complaints-To: groups-abuse@google.com NNTP-Posting-Date: Sat, 11 Aug 2007 18:51:12 +0000 (UTC) In-Reply-To: <1186851804.567302.223160@q4g2000prc.googlegroups.com> User-Agent: G2/1.0 X-HTTP-UserAgent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.6) Gecko/20070725 Firefox/2.0.0.6,gzip(gfe),gzip(gfe) Complaints-To: groups-abuse@google.com Injection-Info: e9g2000prf.googlegroups.com; posting-host=75.70.221.169; posting-account=ps2QrAMAAAA6_jCuRt2JEIpn5Otqf_w0 Xref: g2news2.google.com comp.lang.ada:1402 Date: 2007-08-11T11:51:11-07:00 List-Id: On Aug 11, 11:03 am, shaunpatter...@gmail.com wrote: > I'm having trouble with tasking in Ada. I'm used to working with > pthreads > in C/C++... and I'm finding tasks to be somewhat different/annoying. > > My basic problem is I want to have 2 threads in my program. > > One thread sits in the background and reads in messages, > dispatches them, etc and the other thread reads off a queue > and sends out messages when they are available. > > As an analogy, let's assume my background task just got > input from the user (simulating the socket read) and the other > task just printed out stuff. You need to communicate between the tasks. Ada provides many ways for tasks to communicate. The easiest to use are task rendezvous and protected objects. The following example demonstrates the use of a protected object: with Ada.Text_Io; with Ada.strings.Unbounded; procedure Coordinate_Tasks is use Ada.Strings.Unbounded; protected Buffer is entry Put(Item : in Unbounded_String); entry Get(Item : out Unbounded_String); private Message : Unbounded_String; Is_New : Boolean := False; end Buffer; protected body Buffer is entry Put(Item : in Unbounded_String) when not Is_New is begin Message := Item; Is_New := True; end Put; entry Get(Item : out Unbounded_String) when Is_New is begin Item := Message; Is_New := False; end Get; end Buffer; task Read_Input; task body Read_Input is Input_Value : String(1..1024); Length : Natural; begin loop Ada.Text_IO.Get_Line(Item => Input_Value, Last => Length); Buffer.Put(To_Unbounded_String(Input_Value(1..Length))); end loop; end Read_Input; task Print_Output; task body Print_Output is Buffered_Value : Unbounded_String; begin loop Buffer.Get(Buffered_Value); Ada.Text_Io.Put_Line(To_String(Buffered_Value)); end loop; end Print_Output; begin null; end Coordinate_Tasks; The next example uses the rendezvous mechanism allowing tasks to communicate directly with each other: with Ada.Text_Io; with Ada.Strings.Unbounded; procedure Task_Coordination_2 is use Ada.Strings.Unbounded; task Read_Input; task Print_Value is entry Put(Item : Unbounded_String); end Print_Value; task body Read_Input is Input_Value : String(1..1024); Length : Natural; begin loop Ada.Text_Io.Get_Line(Item => Input_Value, Last => Length); Print_Value.Put(To_Unbounded_String(Input_Value(1..Length))); end loop; end Read_Input; task body Print_Value is begin loop accept Put(Item : Unbounded_String) do Ada.Text_Io.Put_Line(To_String(Item)); end Put; end loop; end Print_Value; begin null; end Task_Coordination_2; Jim Rogers