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=-0.9 required=5.0 tests=BAYES_00,FORGED_GMAIL_RCVD, FREEMAIL_FROM autolearn=no autolearn_force=no version=3.4.4 Path: border2.nntp.dca1.giganews.com!border1.nntp.dca1.giganews.com!nntp.giganews.com!usenet.blueworldhosting.com!feeder01.blueworldhosting.com!feeder.erje.net!eu.feeder.erje.net!eternal-september.org!feeder.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail From: =?ISO-8859-1?Q?Bj=F6rn_Lundin?= Newsgroups: comp.lang.ada Subject: Re: can someone help me with this code (explanation) Date: Fri, 26 Sep 2014 09:04:52 +0200 Organization: A noiseless patient Spider Message-ID: References: <6e1f86e6-c17a-428e-bb19-460c5ba26c8a@googlegroups.com> <1ec7272d-de7f-43dd-be30-009c437011de@googlegroups.com> Mime-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: 8bit Injection-Date: Fri, 26 Sep 2014 07:04:13 +0000 (UTC) Injection-Info: mx05.eternal-september.org; posting-host="23e59b4906029a0ce22afc4c4b1f25ee"; logging-data="23480"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX19vzfSaqslH80hvg61f6ZWQ" User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:24.0) Gecko/20100101 Icedove/24.7.0 In-Reply-To: Cancel-Lock: sha1:Io7XEPvh9jZLY5JbvtztdmtbdQQ= Xref: number.nntp.dca.giganews.com comp.lang.ada:189161 Date: 2014-09-26T09:04:52+02:00 List-Id: On 2014-09-26 00:27, Jeffrey Carter wrote: > On 09/25/2014 01:10 PM, Niklas Holsti wrote: >> >> To make task-safe use of Ada.Text_IO, it should be wrapped in some way >> to ensure that only one task calls it at a time. For example, you could >> write a task with an entry like Put_Line (Message : in String) and an >> accept statement that does Ada.Text_IO.Put_Line (Message). But a >> protected-object wrapper is probably better for uses where I/O >> performance (latency) is not critical. > > Except that I/O is potentially blocking, and so cannot appear in a protected > operation, meaning one needs a task one way or another. > Or by using a protected object as a semaphore protected type Semaphore_Type is procedure Release; entry Seize; private In_Use : Boolean := False; end Semaphore_Type; protected body Semaphore_Type is procedure Release is begin In_Use := False; end; entry Seize when not In_Use is begin In_Use := True; end; end Semaphore_Type; Semaphore : Semaphore_Type; procedure Do_Safe_Put_Line(What : String) is begin Semaphore.Seize; Ada.Text_Io.Put_Line(What); Semaphore.Release; end Do_Safe_Put_Line; procedure Do_Safe_Put(What : String) is begin Semaphore.Seize; Ada.Text_Io.Put(What); Semaphore.Release; end Do_Safe_Put; Using Do_Safe_Put or Do_Safe_Put_Line can be used by any of the tasks. If several tries to use them at the same time, they will line up in the Seize statement, and go ahead whe the task holding the semaphor calls Release. -- Björn