v2026.11.2
All Bundles
Bundle

System support for threads and runtime concurrency

Semaphore

Counting semaphore: a pool of permits. Acquire takes one, waiting while none are left; Release returns one and wakes a waiting thread. Built on a ThreadMutex and a Condition, so a waiting thread sleeps rather than spins.

Example

sem := Semaphore->New(2);
sem->Acquire();
sem->Available()->PrintLine(); # 1
sem->TryAcquire()->PrintLine(); # true
sem->TryAcquire()->PrintLine(); # false
sem->Release();
sem->Release();
sem->Available()->PrintLine(); # 2

Operations

Acquire #

Takes a permit, waiting until one is available

method : public : Acquire() ~ Nil

Available #

Number of permits available now

method : public : Available() ~ Int

Return

TypeDescription
Intnumber of permits

New # constructor

Creates a semaphore

New(permits:Int)

Parameters

NameTypeDescription
permitsIntnumber of permits available at the start (a negative number counts as 0)

Release #

Returns a permit and wakes one waiting thread

method : public : Release() ~ Nil

Release #

Returns several permits and wakes the waiting threads

method : public : Release(count:Int) ~ Nil

Parameters

NameTypeDescription
countIntnumber of permits to return (nothing happens unless it is positive)

TryAcquire #

Takes a permit if one is available, without waiting

method : public : TryAcquire() ~ Bool

Return

TypeDescription
Booltrue if a permit was taken, false otherwise