comp.lang.ada
 help / color / mirror / Atom feed
From: Andrew Shvets <andrew.shvets@gmail.com>
Subject: Re: How to simulate semaphores with Ada's tasks?
Date: Sat, 27 Aug 2016 18:59:41 -0700 (PDT)
Date: 2016-08-27T18:59:41-07:00	[thread overview]
Message-ID: <e5de6d99-8e73-43ad-ae15-548920dac1dc@googlegroups.com> (raw)
In-Reply-To: <nptdan$2t8$1@dont-email.me>

On Saturday, August 27, 2016 at 9:04:26 PM UTC-4, Jeffrey R. Carter wrote:
> On 08/27/2016 02:36 PM, Andrew Shvets wrote:
> > 
> > My current understanding is that the protected type is a way to encapsulate a
> > piece of data and then regulate which values can be assigned its insides
> > (similar to how in Java there is a way to create a piece of code where only
> > one thread at a time can run at any given moment.)  The example below has 5
> > tasks and each one tries to update the same unbounded string.  The goal was
> > to create a small application with 5 tasks and each one fighting to update
> > the same value.
> > 
> > Does my description make sense?  Or am I completely off-base?
> 
> In general, a protected object encapsulates a set of operations that are
> mutually exclusive. Only one call to one operation may proceed at a time. (This
> is not strictly correct, but it's close enough, it's simple, and an
> implementation that works this way conforms to the standard.) It may also
> encapsulate and hide some optional data that the operations operate on.
> 
> The PO in your example, as written, implements a standard monitor with a default
> initial value for Unbounded_String (the default initial value is ""). A monitor
> stores one value; calls to Put a new value overwrite the existing value; calls
> to Get the value leave the value unaffected, and future calls to Get can return
> the same value. This is sometimes described as having a destructive Put and
> non-destructive Get. A queue, on the other hand, has a non-destructive Put and a
> destructive Get. Your Empty component serves no purpose. If you had barriers
> using Empty as in your comments, then your PO would be a bounded, blocking queue
> with a maximum length of 1.
> 
> So from that point of view, you seem to understand how to use POs.
> 
> There's special syntax for singleton POs like yours:
> 
> protected Protected_01 is
>    ...
> end Protected_01;
> 
> This is equivalent to
> 
> protected type /anonymous/ is
>    ...
> end /anonymous/;
> 
> Protected_01 : /anonymous/;
> 
> If I were implementing the same functionality, I'd probably use String
> externally and only use Unbounded_String when needed to store a String value:
> 
> protected Protected_String is
>    procedure Put (Value : in String);
>    function Value return String;
> private -- Protected_String
>    Current : Unbounded_String;
> end Protected_String;
> 
> The body would convert as needed.
> 
> Looking at the rest of the example, your task is confused. The loop is executed
> exactly once, so there's no reason for it. Your select has only a single
> alternative, so there's no reason for it. Stripped of the output statements,
> it's functionality is
> 
> begin
>    accept Start ...;
>    accept Quit;
>    Protected_01.Insert ...;
> end;
> 
> The default value of an uninitialized Unbounded_String is the null string, so given
> 
> V : Unbounded_String;
> 
> V's value is "" and there's no reason to explicitly initialize it to that.
> However, if you do need to refer to the null string value, you can use
> Null_Unbounded_String, which is probably clearer and better than
> To_Unbounded_String ("").
> 
> There's no guarantee that multiple tasks writing to the same
> Ada.Text_IO.File_Type will give you useful results.
> 
> -- 
> Jeff Carter
> "Monsieur Arthur King, who has the brain of a duck, you know."
> Monty Python & the Holy Grail
> 09

You are correct, the loop runs only once.  It should be like so:

else
  Ada.Text_IO.Put_Line("Task is inserting!");
  Protected_01.Insert(Task_Custom_String);
  delay 0.0;
end select;

  reply	other threads:[~2016-08-28  1:59 UTC|newest]

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2016-08-25  0:41 How to simulate semaphores with Ada's tasks? Andrew Shvets
2016-08-25  1:04 ` Jeffrey R. Carter
2016-08-25  1:44   ` Andrew Shvets
2016-08-27 21:36   ` Andrew Shvets
2016-08-28  1:04     ` Jeffrey R. Carter
2016-08-28  1:59       ` Andrew Shvets [this message]
2016-08-25  7:39 ` Dmitry A. Kazakov
2016-08-27 18:06   ` Andrew Shvets
2016-08-25  7:55 ` G.B.
replies disabled

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