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 autolearn=unavailable autolearn_force=no version=3.4.4 Path: eternal-september.org!reader01.eternal-september.org!reader02.eternal-september.org!news.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail From: "Jeffrey R. Carter" Newsgroups: comp.lang.ada Subject: Re: How to simulate semaphores with Ada's tasks? Date: Sat, 27 Aug 2016 18:04:21 -0700 Organization: Also freenews.netfront.net; news.tornevall.net; news.eternal-september.org Message-ID: References: <35ae841e-5947-44e9-a8d4-479cf40c4277@googlegroups.com> <7b1bb0e6-6a87-476a-978f-dbfec7bb638a@googlegroups.com> Mime-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 7bit Injection-Date: Sun, 28 Aug 2016 01:04:25 -0000 (UTC) Injection-Info: mx02.eternal-september.org; posting-host="d7c030f56102b58a2c16dea977db88bb"; logging-data="2984"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX193Nl+Y6nJTNLnk4blcOepDs5N7bj+WEX8=" User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:45.0) Gecko/20100101 Thunderbird/45.2.0 In-Reply-To: <7b1bb0e6-6a87-476a-978f-dbfec7bb638a@googlegroups.com> X-Mozilla-News-Host: news://news.eternal-september.org Cancel-Lock: sha1:gGTVflJ1PWH7g1Ym1qK3udYaiok= Xref: news.eternal-september.org comp.lang.ada:31616 Date: 2016-08-27T18:04:21-07:00 List-Id: 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