comp.lang.ada
 help / color / mirror / Atom feed
From: Robert A Duff <bobduff@shell01.TheWorld.com>
Subject: Re: Strange behavior
Date: Fri, 05 Sep 2014 16:34:41 -0400
Date: 2014-09-05T16:34:41-04:00	[thread overview]
Message-ID: <wcc8ulxyffi.fsf@shell01.TheWorld.com> (raw)
In-Reply-To: 6967b17b-acb9-4b44-b21a-6ddcab1e1065@googlegroups.com

Laurent <daemon2@internet.lu> writes:

> with Ada.Text_IO;
> procedure Two_Cooperating_Tasks is
>    
>    task type Simple_Task (Message : Character; How_Many : Positive);
>    
>    task body Simple_Task is
>       
>    begin -- Simple_Task
>       
>       for Count in 1 .. How_Many loop
>          Ada.Text_IO.Put ("Hello from Task " & Message);
>          Ada.Text_IO.New_Line;
>          delay 0.1;
>       end loop;
>       
>    end Simple_Task;
>    
>    Task_A : Simple_Task (Message => 'A', How_Many => 5);
>    Task_B : Simple_Task (Message => 'B', How_Many => 5);
>    
> begin -- Two_Cooperating_Tasks
>    
>    null;
>    
> end Two_Cooperating_Tasks;

The above causes erroneous execution, which means that anything can
happen.  Two tasks are writing to standard output without proper
synchronization.  (I would prefer the language defined each output
call to be atomic, but it doesn't.  And that wouldn't help much in your
example, because you have two calls, Put and New_Line.  You could
combine those into a single Put_Line, by the way.)

If you have only one processor, then the output shown in the text is
likely to happen.  But you can't depend on that.  And anyway, nowadays
you almost certainly have more than one processor, so the tasks are
executing in parallel.

Synchronization via delay statements is a wrong way to do.

- Bob


  parent reply	other threads:[~2014-09-05 20:34 UTC|newest]

Thread overview: 10+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2014-09-05 19:30 Strange behavior Laurent
2014-09-05 19:47 ` sbelmont700
2014-09-05 20:05   ` Laurent
2014-09-06  0:53     ` Dennis Lee Bieber
2014-09-06  1:35       ` Adam Beneschan
2014-09-05 20:23 ` Jeffrey Carter
2014-09-05 20:48   ` Robert A Duff
2014-09-05 20:34 ` Robert A Duff [this message]
2014-09-05 20:40   ` Laurent
2014-09-06  1:10     ` Dennis Lee Bieber
replies disabled

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