comp.lang.ada
 help / color / mirror / Atom feed
From: Jeffrey Carter <spam.jrcarter.not@spam.not.acm.org>
Subject: Re: Simp,e example for 2 tasks
Date: Sun, 21 Sep 2014 13:33:29 -0700
Date: 2014-09-21T13:33:29-07:00	[thread overview]
Message-ID: <lvncmq$iim$1@dont-email.me> (raw)
In-Reply-To: <VuDTv.262712$_k.68089@fx16.iad>

On 2014-09-21 7:31 AM, Stribor40 wrote:
> Would anyone be able to post simple example of ada program showing 2 tasks
> taking to each other please.  For example one task sends message to another
> saying "hi" and second taks replying "hi back".

As others have pointed out, Ada tasking is based on synchronous communication,
not the message queues found in other languages. This why Ada had to introduce
the partition concept to achieve distribution, while Erlang processes distribute
quite nicely by themselves, as the ping-pong example shows.

It follows that to have message passing in Ada one must create the mechanism
that stores a message from one task and allows another task to retrieve it. In
Ada 12 one can use the synchronized-queue containers for that.

with Ada.Containers.Synchronized_Queue_Interfaces;
with Ada.Containers.Bounded_Synchronized_Queues;
with Ada.Strings.Unbounded;

procedure Task_Example is
   package String_Queue_IF is new Ada.Containers.Synchronized_Queue_Interfaces
      (Element_Type => Ada.Strings.Unbounded.Unbounded_String);
   package String_Queues is new Ada.Containers.Bounded_Synchronized_Queues
      (Queue_Interfaces => String_Queue_IF, Default_Capacity => 1);

   A_To_B : String_Queues.Queue;
   B_To_A : String_Queues.Queue;

   task A;
   task B;

   task body A is
      Message : Ada.Strings.Unbounded.Unbounded_String;
   begin -- A
      A_To_B.Enqueue (New_Item => Ada.Strings.Unbounded.To_String ("Hi") );
      B_To_A.Dequeue (Element => Message);
   end A;

   task body B is
      Message : Ada.Strings.Unbounded.Unbounded_String;
   begin -- B
      A_To_B.Dequeue (Element => Message);
      B_To_A.Enqueue (New_Item => Ada.Strings.Unbounded.To_String ("Hi back") );
   end B;
begin -- Task_Example
   null;
end Task_Example;

-- 
Jeff Carter
"How'd you like to hide the egg and gurgitate
a few saucers of mocha java?"
Never Give a Sucker an Even Break
101


  reply	other threads:[~2014-09-21 20:33 UTC|newest]

Thread overview: 12+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2014-09-21 13:31 Simp,e example for 2 tasks Stribor40
2014-09-21 15:59 ` Dennis Lee Bieber
2014-09-21 16:03   ` Stribor40
2014-09-21 22:26     ` Dennis Lee Bieber
2014-09-21 16:09 ` Dmitry A. Kazakov
2014-09-21 16:28 ` Stribor40
2014-09-21 17:00 ` Brad Moore
2014-09-21 20:33   ` Jeffrey Carter [this message]
2014-09-22  7:18     ` Niklas Holsti
2014-09-22 17:48       ` Jeffrey Carter
2014-09-22 18:16         ` Niklas Holsti
2014-09-22 19:27           ` Dmitry A. Kazakov
replies disabled

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