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!feeder.eternal-september.org!aioe.org!.POSTED!not-for-mail From: "Dmitry A. Kazakov" Newsgroups: comp.lang.ada Subject: Re: How to simulate semaphores with Ada's tasks? Date: Thu, 25 Aug 2016 09:39:01 +0200 Organization: Aioe.org NNTP Server Message-ID: References: <35ae841e-5947-44e9-a8d4-479cf40c4277@googlegroups.com> NNTP-Posting-Host: vZYCW951TbFitc4GdEwQJg.user.gioia.aioe.org Mime-Version: 1.0 Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 7bit X-Complaints-To: abuse@aioe.org User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:45.0) Gecko/20100101 Thunderbird/45.2.0 X-Notice: Filtered by postfilter v. 0.8.2 Xref: news.eternal-september.org comp.lang.ada:31561 Date: 2016-08-25T09:39:01+02:00 List-Id: On 25/08/2016 02:41, Andrew Shvets wrote: > From what I've read, tasks do not have semaphores that can permit it > to lock a piece of memory and prevent it from being accessed, is this > correct? It is unclear what you mean here. Semaphore blocks a resource, not necessarily memory. Technically you cannot block memory at the language level, e.g. using memory management registers. It is too low-level. So it is operations that are blocked with memory encapsulated somewhere into an object or a package. > If so, then the best possible way to simulate this is to have tasks > send messages to one another and synchronize when something is > locked/unlocked. That is called "monitor". It can be used to implement the semaphore too, but nobody does that. In most cases monitor is better than semaphore when that comes in question. Semaphore has little if no use when k>1. Only mutex (semaphore k=1) is actively used. > Is there a better approach? Sometimes monitor is better sometimes mutex is. Regarding mutex what kind of are you looking for? Plain? Reentrant? Read-write mutex? Arrays of mutexes? In Ada synchronization objects like mutex are implemented using protected object. A plain mutex or semaphore is trivial to implement. -- Regards, Dmitry A. Kazakov http://www.dmitry-kazakov.de