[fpc-pascal] Counting semaphore for free pascal

Sven Barth pascaldragon at googlemail.com
Wed Jul 29 15:21:07 CEST 2015


Am 28.07.2015 20:14 schrieb "kapibara" <kapibara.pas at aol.com>:
>
> Hi there,
>
> A counting, or general, semaphore limits simultaneous access to a
resource according to the number of permits you specify. On the other hand,
a binary semaphore like a critical section limits the access to "one at a
time".
>
> FPC doesn't seem to have a general semaphore? I recently attempted to
implement one, here is the code, plus a working Lazarus demo attached. It
blocks on an event instead of a critical section. However, I didnt manage
to have it block by just calling Wait, because then the code blocked
without leaving the PermitLock and that meant deadlock for all threads. So
thats why you see the "if Semaphore.Wait(aEvent) then
RTLEventWaitFor(aEvent)" instead of just Semaphore.Wait(aEvent). It works
just fine, but if anyone knows how to implement this by just calling
Wait(aEvent), please tell. If you want to block on a critical section
instead of an event, do that. Perhaps the result could be TFPSemaphore and
go with FPC.

Any TSemaphore implementation should provide the same methods that Delphi's
TSemaphore provides:
http://docwiki.embarcadero.com/Libraries/XE6/en/System.SyncObjs.TSemaphore

Regards,
Sven
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.freepascal.org/pipermail/fpc-pascal/attachments/20150729/0cea9fb5/attachment.html>


More information about the fpc-pascal mailing list