From: "jimmaureenrogers@worldnet.att.net" <jimmaureenrogers@worldnet.att.net>
Subject: Re: Tasking issues
Date: Sat, 11 Aug 2007 11:51:11 -0700
Date: 2007-08-11T11:51:11-07:00 [thread overview]
Message-ID: <1186858271.483486.112630@e9g2000prf.googlegroups.com> (raw)
In-Reply-To: <1186851804.567302.223160@q4g2000prc.googlegroups.com>
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
next prev parent reply other threads:[~2007-08-11 18:51 UTC|newest]
Thread overview: 27+ messages / expand[flat|nested] mbox.gz Atom feed top
2007-08-11 17:03 Tasking issues shaunpatterson
2007-08-11 18:42 ` Dmitry A. Kazakov
2007-08-12 11:06 ` Simon Wright
2007-08-12 12:05 ` Dmitry A. Kazakov
2007-08-12 17:12 ` shaunpatterson
2007-08-12 18:03 ` Dmitry A. Kazakov
2007-08-12 22:10 ` Jeffrey R. Carter
2007-08-13 19:54 ` Simon Wright
2007-08-13 22:30 ` shaunpatterson
2007-08-14 7:10 ` Tasking issues => Book List anon
[not found] ` <13bulskfjvogv8e@corp.supernews.com>
2007-08-12 20:20 ` Tasking issues Simon Wright
2007-08-11 18:51 ` jimmaureenrogers [this message]
2007-08-11 19:08 ` Jeffrey R. Carter
2007-08-11 22:31 ` Steve
2007-08-12 9:00 ` anon
2007-08-12 9:43 ` Dmitry A. Kazakov
2007-08-12 21:39 ` anon
2007-08-12 22:15 ` Jeffrey R. Carter
2007-08-13 9:13 ` anon
2007-08-13 19:37 ` Simon Wright
2007-08-13 20:17 ` Markus E.L. 2
2007-08-14 0:40 ` Jeffrey R. Carter
2007-08-13 9:22 ` Dmitry A. Kazakov
2007-08-13 12:41 ` Larry Kilgallen
2007-08-13 13:22 ` Dmitry A. Kazakov
2007-08-12 21:03 ` Maciej Sobczak
2007-08-12 22:07 ` Jeffrey R. Carter
replies disabled
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox