comp.lang.ada
 help / color / mirror / Atom feed
From: Brad Moore <brad.moore@shaw.ca>
Subject: Re: Simp,e example for 2 tasks
Date: Sun, 21 Sep 2014 11:00:37 -0600
Date: 2014-09-21T11:00:37-06:00	[thread overview]
Message-ID: <VuDTv.262712$_k.68089@fx16.iad> (raw)
In-Reply-To: <9b22dd09-6137-4f4b-8be1-28255032df70@googlegroups.com>

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".
> I am having troubles finding example of simple program like this online
>

As others have mentioned, the problem needs to be better specified.
My assumptions are;
    1. No content to the message, as none is needed, so no parameters
       are needed for the entry calls
    2. The sender and listener tasks are tightly coupled and know of each
       other, so can send message to each other directly.
    3. The listener needs to send back a separate message to the sender.
       In other words, the response message is not sent as part of the
       rendezvous, and is sent separately. Dmitry's example handles the
       other case where only one rendezvous is needed.

procedure Task_Example
is
    task Listener is
       entry Send_Message;
    end Listener;

    task Sender is
       entry Give_Reply;
    end Sender;

    task body Listener is
    begin
       accept Send_Message;
       Put_Line ("Sender says Hi");
       Sender.Give_Reply;
    end Listener;

    task body Sender is
    begin
       Listener.Send_Message;
       accept Give_Reply;
       Put_Line ("Listener says Hi Back!");
    end Sender;

begin
       null;
end Task_Example;

The task communication here is via task rendezvous. For a more 
complicated example, I might have chosen to use a protected object
to handle the communication instead. I generally prefer to use
protected objects for task communication, over rendezvous.

procedure Task_Example
is
       protected Manager is
          procedure Send_Message;
          entry Wait_For_Message;
          procedure Send_Response;
          entry Wait_For_Response;
       private
          Received_Message : Boolean := False;
          Received_Response : Boolean := False;
       end Manager;

       protected body Manager is
          procedure Send_Message is
          begin
             Received_Message := True;
          end Send_Message;

          entry Wait_For_Message when Received_Message is
          begin
             null;
          end Wait_For_Message;

          procedure Send_Response is
          begin
             Received_Response := True;
          end Send_Response;

          entry Wait_For_Response when Received_Response is
          begin
             null;
          end Wait_For_Response;
       end Manager;

       task Listener;
       task Sender;

       task body Listener is
       begin
          Manager.Wait_For_Message;
          Put_Line ("Sender says Hi");
          Manager.Send_Response;
       end Listener;

       task body Sender is
       begin
          Manager.Send_Message;
          Manager.Wait_For_Response;
          Put_Line ("Listener says Hi Back!");
       end Sender;
begin
    null;
end Task_Example;

Brad


  parent reply	other threads:[~2014-09-21 17:00 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 [this message]
2014-09-21 20:33   ` Jeffrey Carter
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