From: "Egil Høvik" <egilhovik@hotmail.com>
Subject: Re: Newbie: Code isn't executed
Date: 4 Aug 2005 07:48:26 -0700
Date: 2005-08-04T07:48:26-07:00 [thread overview]
Message-ID: <1123166906.832393.147470@f14g2000cwb.googlegroups.com> (raw)
In-Reply-To: <20050804161000.1ffa591b@localhost>
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;
next prev parent reply other threads:[~2005-08-04 14:48 UTC|newest]
Thread overview: 13+ messages / expand[flat|nested] mbox.gz Atom feed top
2005-08-04 14:10 Newbie: Code isn't executed Thomas Ruschival
2005-08-04 14:47 ` Ed Falis
2005-08-04 14:48 ` Egil Høvik [this message]
2005-08-04 15:07 ` Martin Dowie
2005-08-04 17:37 ` tmoran
2005-08-04 20:57 ` Randy Brukardt
2005-08-05 6:11 ` Vinzent 'Gadget' Hoefler
2005-08-06 3:52 ` tmoran
2005-08-04 20:20 ` Simon Wright
2005-08-04 15:09 ` Martin Dowie
2005-08-04 16:37 ` SOLVED: " Thomas Ruschival
2005-08-05 2:43 ` Steve
2005-08-06 5:33 ` Jeffrey Carter
replies disabled
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox