Semaphore

Struct Semaphore 

Source
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: T

Implementations§

Source§

impl<T> Semaphore<T>

Source

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
Source

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.

Source

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.

Auto Trait Implementations§

§

impl<T> Freeze for Semaphore<T>
where T: Freeze,

§

impl<T> RefUnwindSafe for Semaphore<T>
where T: RefUnwindSafe,

§

impl<T> Send for Semaphore<T>
where T: Send,

§

impl<T> Sync for Semaphore<T>
where T: Sync,

§

impl<T> Unpin for Semaphore<T>
where T: Unpin,

§

impl<T> UnwindSafe for Semaphore<T>
where T: UnwindSafe,

Blanket Implementations§

§

impl<T> Any for T
where T: 'static + ?Sized,

§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
§

impl<T> Borrow<T> for T
where T: ?Sized,

§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
§

impl<T> BorrowMut<T> for T
where T: ?Sized,

§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
§

impl<T> From<T> for T

§

fn from(t: T) -> T

Returns the argument unchanged.

§

impl<T, U> Into<U> for T
where U: From<T>,

§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of [From]<T> for U chooses to do.

§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.