comp.lang.ada
 help / color / mirror / Atom feed
* (Newbie) intertask communication
@ 2001-04-06 17:37 Pieter Thysebaert
  2001-04-06 20:46 ` Mark Lundquist
  0 siblings, 1 reply; 2+ messages in thread
From: Pieter Thysebaert @ 2001-04-06 17:37 UTC (permalink / raw)


Hi....

I'm trying to have two tasks interact (using entry/accept).
In fact we have this assignment where we are supposed to (not in reality)
monitor some patient's heartbeat.

So I created two task types : patient and monitor

The idea is that the patient will call the function (entry) beat on his/her
associated monitor;
the monitor will accept beat() calls and will display a warning when not
receiving  a heartbeat for two (or x) seconds

So this is my monitor:

task body monitortask is
   id : Integer;
begin
   accept start(patient_id : Integer) do
     id := patient_id;
   end;
   loop
      select
         accept beat do
             PutLine(<patient id is alivestuff>);
         end;
      or
          delay 2;
          sound_alarm();
   end loop;

end monitortask;

And the patient :

task body patienttask is
begin
   accept start(m : monitortask) do
   loop
      delay <random value between 1 and 4 seconds>;
      m.beat;
   end loop;
   end;
end patienttask;

My problem/question is : this stuff only works when m.start is called first
in a testprogram, end then p.start
(m : monitortask and p : patientask) - the testprogram blosk on the
patient.start thing
That's because I have the loop _IN_ the patient's accept statement.

Of course what I want is more like :


task body patienttask is
  mymonitor : monitortask;
begin
  accept start(m : monitortask) do
     mymonitor := m;
  end;
  loop
.....do stuff but call mymonitor.beat;


which will not block the testprogram calling p.start;

Unfortunately, the assignment to mymonitor does not work like this.

How would I pass the "name" of a task to antoher task to have it use that
one ?
And how can I save a "reference"to a task  ?


Pieter







^ permalink raw reply	[flat|nested] 2+ messages in thread

* Re: (Newbie) intertask communication
  2001-04-06 17:37 (Newbie) intertask communication Pieter Thysebaert
@ 2001-04-06 20:46 ` Mark Lundquist
  0 siblings, 0 replies; 2+ messages in thread
From: Mark Lundquist @ 2001-04-06 20:46 UTC (permalink / raw)



Pieter Thysebaert <pieter.thysebaert@pandora.be> wrote in message
news:q9nz6.15400$ii5.1263786@afrodite.telenet-ops.be...
> Hi....
>
> I'm trying to have two tasks interact (using entry/accept).
> In fact we have this assignment where we are supposed to (not in reality)
> monitor some patient's heartbeat.
>
>
[example snipped...]

OK, this post was a prime example of the right way to ask for help with a
homework assignment!  Thanks, Pieter!

> How would I pass the "name" of a task to antoher task to have it use that
> one ?
> And how can I save a "reference"to a task  ?

Use an access type designating a task type.  This can be used for any formal
parameter (to a "callable entity", i.e. subprogram or entry, or to a
generic), and of course you can declare a variable of this type in your
Patient task and assign to it, to save your reference to the other task.  So
for example:

    task type Monitor is
        .
        .

    type Access_Monitor is access all Monitor;
    .
    .

    Monitor_1 : aliased Monitor;

    Patient_1.Start  (Monitor_1'Access);


Also, a task type can have discriminants.  So you can do this:

    task type Patient (Associated_Monitor : Access_Monitor)

For that matter, it can be an "access discriminant" (then you no longer need
the access type, and you get a built-in null check):

    task type Patient (Associated_Monitor : access Monitor);

Hope this helps,
-- mark






^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2001-04-06 20:46 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2001-04-06 17:37 (Newbie) intertask communication Pieter Thysebaert
2001-04-06 20:46 ` Mark Lundquist

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