pub struct Semaphore<T> {
resource: T,
}Expand description
Counting semaphore.
A semaphore maintains a set of permits and resource. Permits are used to synchronize access to a shared resource. A semaphore differs from a mutex in that it can allow more than one concurrent caller to access the shared resource at a time.
Fields§
§resource: TImplementations§
Source§impl<T> Semaphore<T>
impl<T> Semaphore<T>
Sourcepub fn new(permits: usize, resource: T) -> Self
pub fn new(permits: usize, resource: T) -> Self
Creates a new semaphore initialized with a specified number of permits.
§Arguments
permits- The initial number of available permits. Must be a non-negative number.state- A resource combined with this resource
Sourcepub fn wait(&self) -> SemaphorePermits<'_, T>
pub fn wait(&self) -> SemaphorePermits<'_, T>
Waits until a permit becomes available and then acquires it.
If no permits are available, this function will block the current thread
until another thread calls signal() to release a permit.
This method returns a SemaphorePermits RAII guard. When the guard is
dropped, it will automatically release the acquired permit.
Sourcepub fn signal(&self)
pub fn signal(&self)
Releases a permit back to the semaphore.
This method increases the number of available permits by one, and if any
threads are blocked in wait(), one will be woken up to acquire the
newly released permit.
Normally, you don’t call this directly except for signaling an event
with a zero-initialized semaphore. Instead, it’s automatically invoked
when a SemaphorePermits guard is dropped.