comp.lang.ada
 help / color / mirror / Atom feed
From: Jacob Sparre Andersen <sparre@hugin.crs4.it>
Subject: Re: [newbie question] tasks and protected types
Date: 29 Apr 2005 11:43:37 +0200
Date: 2005-04-29T11:43:37+02:00	[thread overview]
Message-ID: <m23bt9dik6.fsf@hugin.crs4.it> (raw)
In-Reply-To: 1114747457.868019.93210@f14g2000cwb.googlegroups.com

Fabio de Francesco wrote:

[...]

>     G : Generator;

Global objects are a _very_ bad idea when doing tasking.  Move it into
your task type.

>     task body Door is
>         Msg : String := "";

Here you make the lenght of the string Msg 0.

>         Val : Positive;
>         Times : Positive;
>     begin
>         accept Start( Str : in String; Tm : in Positive ) do
>             Msg := Str;

And here you try to assign a string of length 6 to a string of length
0.  It will fail with a Constraint_Error, but with tasking you don't
always see the exceptions.

And then I also prefer to keep the printing to a single protected
object.

Here's a modified version of your program using the advice above (and
my pretty-printing style):

-----
with Ada.Text_IO, Ada.Numerics.Float_Random;
use Ada.Numerics.Float_Random;

procedure Manage is

   protected Printer is
      procedure Put_Line (Item : in     String);
   end Printer;

   protected body Printer is
      procedure Put_Line (Item : in     String) is
      begin
         Ada.Text_IO.Put_Line (Item => Item);
         Ada.Text_IO.Flush;
      end Put_Line;
   end Printer;

   protected Tickets is
      procedure Assign (New_Value :    out Positive);
   private
      Data: Integer := 0;
   end Tickets;

   protected body Tickets is
      procedure Assign (New_Value :    out Positive) is
      begin
         Data := Data + 1;
         New_Value := Data;
      end Assign;
   end Tickets;

   subtype Message is String (1 .. 6);

   task type Door is
      entry Start (Str : in     Message;
                   Tm  : in     Positive);
   end Door;

   task body Door is
      G     : Generator;
      Msg   : Message := "      ";
      Val   : Positive;
      Times : Positive;
   begin
      Reset (G);
      accept Start (Str : in     Message;
                    Tm  : in     Positive) do
         Msg := Str;
         Times := Tm;
      end Start;
      for I in 1..Times loop
         delay Duration (Random (G));
         Tickets.Assign (Val);
         Printer.Put_Line (Msg & " counts " & Natural'Image (Val));
      end loop;
   end Door;

   D1 : Door;
   D2 : Door;

begin
   D1.Start ("Task 1", 6);
   D2.Start ("Task 2", 6);
end Manage;
-----

Greetings,

Jacob
-- 
�It will not be forever. - It will just seem like it.� -- Death




  parent reply	other threads:[~2005-04-29  9:43 UTC|newest]

Thread overview: 33+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2005-04-29  4:04 [newbie question] tasks and protected types fabio de francesco
2005-04-29  7:25 ` tmoran
2005-04-29  9:43 ` Jacob Sparre Andersen [this message]
2005-04-29 10:34   ` Alex R. Mosteo
2005-04-29 11:23     ` Jacob Sparre Andersen
2005-04-29 12:18       ` Adrien Plisson
2005-04-29 12:45         ` Ada.Text_IO and protected objects (Was: [newbie question] tasks and protected types) Jacob Sparre Andersen
2005-04-29 13:22           ` Poul-Erik Andreasen
2005-04-29 14:26           ` Egil H. H�vik
2005-04-29 20:28             ` Jacob Sparre Andersen
2005-04-29 20:39             ` Randy Brukardt
2005-04-29 21:23               ` Lionel Draghi
2005-04-29 22:11                 ` fabio de francesco
2005-04-30  3:45                   ` Jeffrey Carter
2005-04-30  7:21                   ` Lionel Draghi
2005-05-02  8:11                 ` Jean-Pierre Rosen
2005-04-30 12:47               ` Ada.Text_IO and protected objects Stephen Leake
2005-04-29 21:57             ` Ada.Text_IO and protected objects (Was: [newbie question] tasks and protected types) fabio de francesco
2005-04-30  9:07               ` Ada.Text_IO and protected objects Jacob Sparre Andersen
2005-04-30 10:21                 ` Dmitry A. Kazakov
2005-05-02 10:41                   ` fabio de francesco
2005-05-02 14:10                     ` Jacob Sparre Andersen
2005-05-02 16:29                       ` fabio de francesco
2005-04-29 15:23           ` Ada.Text_IO and protected objects (Was: [newbie question] tasks and protected types) Björn Lundin
2005-04-29 12:54       ` [newbie question] tasks and protected types Alex R. Mosteo
2005-04-29 10:47   ` fabio de francesco
2005-04-29 11:33     ` Jacob Sparre Andersen
2005-04-29 12:55       ` Alex R. Mosteo
2005-04-29 14:06         ` Egil H. H�vik
2005-04-29 14:12           ` Egil H. H�vik
2005-04-29 16:23             ` Robert A Duff
2005-04-29 20:19           ` Jacob Sparre Andersen
2005-04-30 11:58           ` Simon Wright
replies disabled

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