Semaphore
Permit counter for waking workers.
A new Semaphore has zero permits. Post(n) adds n; Wait() and successful TryWait() consume one. Thus TryWait() is not a passive readiness query.
Store queue data separately. Before Dispose, coordinate worker shutdown: stop flag, Post to leave waiting, then join. Waiting on the main thread can stall the game. Thread-safe APIs. Counting, both Post overloads and wakeup were tested on Windows; FIFO, stress and threadless builds were not accepted.
Declarations checked against C# SDK 1.0.0. Expand a name to see declarations and documentation.
Constructors1
SemaphoreConstructor
public Semaphore();Creates a counter with zero permits. Wait blocks without one; Post adds permits and Wait/TryWait consume one each. Not a data queue or an owner-tracking recursive mutex. Store data separately with coordinated protection; finish all waiting workers before Dispose.
Methods4
Post2 overloads
public void Post();Adds a positive permit count; one when omitted. With no waiter, permits remain for future Wait/TryWait. Guarantees neither FIFO nor immediate execution of a particular thread. count<=0 emits native diagnostics; the method returns no Error. To stop a worker, wake it after updating a stop flag, then join; Post alone does not terminate arbitrary work.
public void Post(int count = 1);Adds a positive permit count; one when omitted. With no waiter, permits remain for future Wait/TryWait. Guarantees neither FIFO nor immediate execution of a particular thread. count<=0 emits native diagnostics; the method returns no Error. To stop a worker, wake it after updating a stop flag, then join; Post alone does not terminate arbitrary work.
| Name | Type | Default |
|---|---|---|
count | int | 1 |
countPositive permit increment, default 1; not a worker ID.
TryWaitbool
public bool TryWait();Nonblockingly consumes one available permit. True means the count decreased; false means no permit is available now. Not a side-effect-free readiness query. Successful TryWait requires neither a second Wait nor returning a permit unless your protocol explicitly does.
boolTrue after consuming one permit, or false without changing the count.
Waitvoid
public void Wait();Blocks the caller until a permit is available and consumes one. Can wait indefinitely: this API has no timeout/cancellation. Do not use on the main thread without a guaranteed Post source; exclude dependencies on an already blocked thread. Wake and join every worker before disposing the Semaphore.