comp.lang.ada
 help / color / mirror / Atom feed
From: "Dmitry A. Kazakov" <mailbox@dmitry-kazakov.de>
Subject: Re: Simp,e example for 2 tasks
Date: Sun, 21 Sep 2014 18:09:27 +0200
Date: 2014-09-21T18:09:27+02:00	[thread overview]
Message-ID: <17mfa3tl60vaq$.1smp65wtk6b9j$.dlg@40tude.net> (raw)
In-Reply-To: 9b22dd09-6137-4f4b-8be1-28255032df70@googlegroups.com

On Sun, 21 Sep 2014 06:31:28 -0700 (PDT), 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

Because there are problems with that:

1. It is bad design, tight coupling. There should never be so that two
object knew each other. Only one task may know another, e.g. client knows
server, server does not know any clients.

2. Sending messages is not the method of task communication. Tasks have
entry points which are alike to procedures not raw data (messages).

3. Messages as marshaled parameters of some asynchronous requests are not
implemented using tasks. There are protected objects which serve this
purpose in Ada.

4. It is not clear what sort of communication you want. Your description
misses many crucial design details required. E.g. are parameters (messages)
marshaled, is exchange synchronous, how knows whom etc.

Anyway, here is an example of text exchange between two tasks A and B:

with Ada.Strings.Unbounded;  use Ada.Strings.Unbounded;
with Ada.Text_IO;  use Ada.Text_IO; 

procedure Talk is
   task A is
      entry Ask (Message : in out Unbounded_String);
   end A;

   task B is
   end B;
   
   task body A is
   begin
      loop
         select
            accept Ask (Message : in out Unbounded_String) do
               Message := To_Unbounded_String ("Hi back");
            end Ask;
         or terminate;
         end select;
      end loop;
   end A;
   
   task body B is
      Message : Unbounded_String;
   begin
      Message := To_Unbounded_String ("Hi");
      Put_Line ("Send " & To_String (Message));
      A.Ask (Message);
      Put_Line ("Received " & To_String (Message));
   end B;

begin
   null;
end Talk;

-- 
Regards,
Dmitry A. Kazakov
http://www.dmitry-kazakov.de


  parent reply	other threads:[~2014-09-21 16:09 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 [this message]
2014-09-21 16:28 ` Stribor40
2014-09-21 17:00 ` Brad Moore
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