comp.lang.ada
 help / color / mirror / Atom feed
From: Laurent <daemon2@internet.lu>
Subject: Re: Strange behavior
Date: Fri, 5 Sep 2014 13:40:29 -0700 (PDT)
Date: 2014-09-05T13:40:29-07:00	[thread overview]
Message-ID: <2c395fc3-344c-45b3-9fd2-94819d6c0f26@googlegroups.com> (raw)
In-Reply-To: <wcc8ulxyffi.fsf@shell01.TheWorld.com>

Am Freitag, 5. September 2014 22:34:17 UTC+2 schrieb Robert A Duff:
> Laurent 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

Yes I understand that. The following examples in the book actually are about this problem and that's the introduction for the protected types. I was just astonished that a so simple program can already behave so unpredictable. Ok the book was written in 1998 so at this time the result was probably different.

Thanks to all for the enlightenment

Laurent


  reply	other threads:[~2014-09-05 20:40 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
2014-09-05 20:40   ` Laurent [this message]
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