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,FREEMAIL_FROM autolearn=ham autolearn_force=no version=3.4.4 X-Google-Thread: 103376,651d9b748faab5c6 X-Google-Attributes: gid103376,public X-Google-Language: ENGLISH,ASCII-7-bit Path: g2news1.google.com!postnews.google.com!f14g2000cwb.googlegroups.com!not-for-mail From: "=?iso-8859-1?B?RWdpbCBI+HZpaw==?=" Newsgroups: comp.lang.ada Subject: Re: Newbie: Code isn't executed Date: 4 Aug 2005 07:48:26 -0700 Organization: http://groups.google.com Message-ID: <1123166906.832393.147470@f14g2000cwb.googlegroups.com> References: <20050804161000.1ffa591b@localhost> NNTP-Posting-Host: 193.71.180.102 Mime-Version: 1.0 Content-Type: text/plain; charset="iso-8859-1" X-Trace: posting.google.com 1123166913 4890 127.0.0.1 (4 Aug 2005 14:48:33 GMT) X-Complaints-To: groups-abuse@google.com NNTP-Posting-Date: Thu, 4 Aug 2005 14:48:33 +0000 (UTC) In-Reply-To: <20050804161000.1ffa591b@localhost> User-Agent: G2/0.2 Complaints-To: groups-abuse@google.com Injection-Info: f14g2000cwb.googlegroups.com; posting-host=193.71.180.102; posting-account=-IK6ow0AAABBM88jphxrP5vRz3uZJ3pJ Xref: g2news1.google.com comp.lang.ada:3958 Date: 2005-08-04T07:48:26-07:00 List-Id: Thomas Ruschival wrote: > Hello Ada group > I am a student learning Ada at university. I played with some tasks > that want to join another task for a rendevouz, its a modiefied exaple > from the lectures. Basically I let 2 tasks sleep a different amount of > time then wake up and execute a piece of code with another task during > a rendevouz, I count the number how often a task got executed. > Somehow a piece of code never gets executed - neither me nor nor my > tutor has a clue why -- here is the code: > > Thanks for _ANY_ comments - I know this isn't the best way of coding - > but it was just meant as an example until strange things happened > > Thomas > Adding an exception handler in the Susan task reveals a CONSTRAINT_ERROR when Total becomes 0. You should change Total to be of type Natural instead of Positive (which can never be 0). Also you have a possible race condition when using the Partner variable which could give you wrong statistics. See comments in your code below. Ada.Text_IO provides a Put_Line procedure so you can write Ada.Text_IO.Put_Line("..."); instead of Ada.Text_IO.Put("..."); Ada.Text_IO.New_Line; > > with Ada.Text_IO; > use Ada.Text_IO; > > procedure myfirst > is > type Name is (Peter,Michael); > type Statistics is array(Name) of Integer; > Partner : Name; > Stat : Statistics :=(others => 0); > Total: Positive :=1000; Should be Total : Natural; > > task Michael_task; > task Peter_task; > task Susan is > entry Dinner; To avoid a possible race condition you probably want peter and michael to tell susan who they are directly in the rendezvous, and not use the "global" variable Partner: entry Dinner( Partner : Name ); > end Susan; > > > task body Peter_task > is > begin > while Total > 0 loop > Put("Peters turn"); > New_Line; > Partner := Peter; > Susan.Dinner; Susan.Dinner(Peter); > delay 0.002; > end loop; > end Peter_task; > > task body Michael_task is > begin > while Total > 0 loop > Put("Michaels turn"); > New_Line; > Partner := Michael; > Susan.Dinner; Susan.Dinner(Michael); > delay 0.001; > end loop; > end Michael_task; > > > task body Susan > is > begin > > while Total > 0 loop > Put("susans turn"); > New_Line; > accept Dinner do accept Dinner( Parnter : Name ) do > stat(Partner) := stat(Partner)+1; > Put("Susan is having dinner with...."& Name'Image(Partner)); > New_Line; > end Dinner; > Total := Total-1; > end loop; > > -- THIS WILL NEVER BE EXECUTED --- WHY ?????????????????????? > Put("Statistics:"); > New_Line; > Put("---------------------------------------------"); > New_Line; > Put("Peter: "&Integer'Image(Stat(Peter))); > New_Line; > Put("Michael: "&Integer'Image(Stat(Michael))); > New_Line; > Put("---------------------------------------------"); > New_Line; > > end Susan; > > > begin -- Procedure > null; > end myfirst;