Skip to content
FutureBlitz

    Install

    Import std.concurrent.locks

    Semaphore

    std.concurrent.locks

    class

    Public Shared Class Semaphore

    Permits that threads take and give back: Acquire takes one, waiting while there's none, and Release gives them back, so no more threads than there are permits go on at once.

    Examples

    Const permits = Semaphore(2)
    Expect(permits.TryAcquire() && permits.TryAcquire())
    Expect(!permits.TryAcquire())
    permits.Release()
    Expect(permits.Available == 1)

    std/concurrent/locks.fb, line 58

    • @PanicsAtCaller Public New(count: Int) constructor #

      A Semaphore with count permits. Panics when that's negative.

    • Public Method Acquire() method #

      Takes a permit, waiting while there's none.

    • Public Method Acquire(cancel: Cancellation) Throws CancelledError method #

      Takes a permit, waiting while there's none, unless the cancellation is cancelled first, or is already, which throws a CancelledError and takes none.

    • Public Method TryAcquire(): Bool method #

      Takes a permit unless there's none, and tells whether it did.

    • @PanicsAtCaller Public Method Release(count: Int = 1) method #

      Gives back count permits, which needn't be ones Acquire took, waking as many threads waiting for one. Panics when count isn't 1 or more, or when it would take the permits past what an Int holds.

    • Public Property Available: Int property #

      How many permits there are now, which other threads may change at any time.

    Report an issue with this page or with FutureBlitz on GitHub.