pub struct Semaphore { /* private fields */ }
Expand description
A type that asynchronously distributes “permits.”
A permit is a token that allows the holder to perform some action. The semaphore itself does not dictate this action, but instead handles only the distribution of permits. This is useful for cases where you want to limit the number of concurrent operations, such as in a mutex.
n
permits can be acquired through acquire
.
They can later be released through release
.
§Examples
For examples, look at the implementations of Mutex
and RwLock
.
Mutex
uses a semaphore with a maximum of 1 permit to allow a single lock at a time.
RwLock
uses a semaphore with a maximum of max_readers
permits to allow any number of readers.
When a write
call is encountered, it acquires all of the permits, blocking any new readers from locking.