Semaphore class
A counting semaphore that bounds how many operations run at once.
Permits are handed to waiters in FIFO order, so a long queue cannot starve its earliest members.
Prefer withPermit over a manual acquire/release pair — it releases the permit even when the action throws.
Example:
final uploads = Semaphore(3);
await Future.wait([
for (final file in files) uploads.withPermit(() => upload(file)),
]);
// At most three uploads are in flight at any moment.
Properties
- available → int
-
Number of permits currently free.
no setter
- hashCode → int
-
The hash code for this object.
no setterinherited
- permits → int
-
The maximum number of permits held by this semaphore.
final
- queueLength → int
-
Number of callers waiting for a permit.
no setter
- runtimeType → Type
-
A representation of the runtime type of the object.
no setterinherited
Methods
-
acquire(
) → Future< void> - Acquires a permit, waiting if none is free.
-
noSuchMethod(
Invocation invocation) → dynamic -
Invoked when a nonexistent method or property is accessed.
inherited
-
release(
) → void - Returns a permit to the semaphore.
-
toString(
) → String -
A string representation of this object.
inherited
-
withPermit<
T> (FutureOr< T> action()) → Future<T> -
Runs
actionwhile holding a permit, releasing it afterwards.
Operators
-
operator ==(
Object other) → bool -
The equality operator.
inherited