From mboxrd@z Thu Jan 1 00:00:00 1970 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on polar.synack.me X-Spam-Level: X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00 autolearn=unavailable autolearn_force=no version=3.4.4 Path: border2.nntp.dca1.giganews.com!nntp.giganews.com!usenet.blueworldhosting.com!feeder01.blueworldhosting.com!news.glorb.com!feed.news.qwest.net!mpls-nntp-01.inet.qwest.net!132.250.1.121.MISMATCH!ra.nrl.navy.mil!bloom-beacon.mit.edu!bloom-beacon.mit.edu!newsswitch.lcs.mit.edu!nntp.TheWorld.com!.POSTED!not-for-mail From: Robert A Duff Newsgroups: comp.lang.ada Subject: Re: Strange behavior Date: Fri, 05 Sep 2014 16:34:41 -0400 Organization: The World Public Access UNIX, Brookline, MA Message-ID: References: <6967b17b-acb9-4b44-b21a-6ddcab1e1065@googlegroups.com> NNTP-Posting-Host: shell01.theworld.com Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii X-Trace: pcls7.std.com 1409949254 13755 192.74.137.71 (5 Sep 2014 20:34:14 GMT) X-Complaints-To: abuse@TheWorld.com NNTP-Posting-Date: Fri, 5 Sep 2014 20:34:14 +0000 (UTC) User-Agent: Gnus/5.1008 (Gnus v5.10.8) Emacs/21.3 (irix) Cancel-Lock: sha1:AD/ClZhJGk1Zv86Qg9PgzrwZ1+c= Xref: number.nntp.dca.giganews.com comp.lang.ada:188883 Date: 2014-09-05T16:34:41-04:00 List-Id: 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