Struct Semaphore

Source
pub struct Semaphore { /* private fields */ }
Expand description

A counting, blocking, semaphore.

Semaphores are a form of atomic counter where access is only granted if the counter is a positive value. Each acquisition will block the calling thread until the counter is positive, and each release will increment the counter and unblock any threads if necessary.

§Examples

use std_semaphore::Semaphore;

// Create a semaphore that represents 5 resources
let sem = Semaphore::new(5);

// Acquire one of the resources
sem.acquire();

// Acquire one of the resources for a limited period of time
{
    let _guard = sem.access();
    // ...
} // resources is released here

// Release our initially acquired resource
sem.release();

Implementations§

Source§

impl Semaphore

Source

pub fn new(count: isize) -> Semaphore

Creates a new semaphore with the initial count specified.

The count specified can be thought of as a number of resources, and a call to acquire or access will block until at least one resource is available. It is valid to initialize a semaphore with a negative count.

Source

pub fn acquire(&self)

Acquires a resource of this semaphore, blocking the current thread until it can do so.

This method will block until the internal count of the semaphore is at least 1.

Source

pub fn release(&self)

Release a resource from this semaphore.

This will increment the number of resources in this semaphore by 1 and will notify any pending waiters in acquire or access if necessary.

Source

pub fn access(&self) -> SemaphoreGuard<'_>

Acquires a resource of this semaphore, returning an RAII guard to release the semaphore when dropped.

This function is semantically equivalent to an acquire followed by a release when the guard returned is dropped.

Auto Trait Implementations§

Blanket Implementations§

Source§

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

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

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

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

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

Source§

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

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

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

Source§

fn into(self) -> U

Calls U::from(self).

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

Source§

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

Source§

type Error = Infallible

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

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

Performs the conversion.
Source§

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

Source§

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

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

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

Performs the conversion.