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=unavailable autolearn_force=no version=3.4.4 X-Received: by 10.13.203.68 with SMTP id n65mr8177935ywd.26.1472349581785; Sat, 27 Aug 2016 18:59:41 -0700 (PDT) X-Received: by 10.157.51.45 with SMTP id f42mr741616otc.12.1472349581728; Sat, 27 Aug 2016 18:59:41 -0700 (PDT) Path: eternal-september.org!reader01.eternal-september.org!reader02.eternal-september.org!news.eternal-september.org!news.eternal-september.org!feeder.eternal-september.org!news.glorb.com!c52no11381916qte.1!news-out.google.com!d68ni41224ith.0!nntp.google.com!x131no543073ite.0!postnews.google.com!glegroupsg2000goo.googlegroups.com!not-for-mail Newsgroups: comp.lang.ada Date: Sat, 27 Aug 2016 18:59:41 -0700 (PDT) In-Reply-To: Complaints-To: groups-abuse@google.com Injection-Info: glegroupsg2000goo.googlegroups.com; posting-host=2601:18f:900:8350:8c29:ad85:baa8:d1ae; posting-account=3pYsyQoAAACcI-ym7XtMOI2PDU8gRZS5 NNTP-Posting-Host: 2601:18f:900:8350:8c29:ad85:baa8:d1ae References: <35ae841e-5947-44e9-a8d4-479cf40c4277@googlegroups.com> <7b1bb0e6-6a87-476a-978f-dbfec7bb638a@googlegroups.com> User-Agent: G2/1.0 MIME-Version: 1.0 Message-ID: Subject: Re: How to simulate semaphores with Ada's tasks? From: Andrew Shvets Injection-Date: Sun, 28 Aug 2016 01:59:41 +0000 Content-Type: text/plain; charset=UTF-8 Xref: news.eternal-september.org comp.lang.ada:31621 Date: 2016-08-27T18:59:41-07:00 List-Id: 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;