Skip to main content

Module semaphore

Module semaphore 

Source
Expand description

Limit concurrent access with a set of permits.

Semaphore::acquire waits for the requested number of permits and returns a guard that puts them back when dropped. Semaphore::try_acquire performs the same operation without waiting, while Semaphore::release adds permits that were not represented by a guard.

§Examples

use asyncband::semaphore::Semaphore;

let semaphore = Semaphore::new(3);
let a_permit = semaphore.acquire(1).await;
let two_permits = semaphore.acquire(2).await;

assert_eq!(semaphore.available_permits(), 0);

let permit_attempt = semaphore.try_acquire(1);
assert!(permit_attempt.is_none());

drop(a_permit);
assert_eq!(semaphore.available_permits(), 1);

Structs§

OwnedSemaphorePermit
An owned permit from the semaphore.
Semaphore
An async counting semaphore for controlling access to a set of resources.
SemaphorePermit
A permit from the semaphore.