simple_semaphore

Struct Semaphore

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

A Semaphore maintains the number of permits it is still allowed to give.

  • When acquire() is called and the semaphore still has permits to give, it will return a Permit. If there are no permits that can be given, it will wait for one permit to be given back from a thread so that it can return a new Permit.
  • When try_acquire() is called and the semaphore still has permits to give, it will return Some(Permit). If there are no permits that can be given, it will return None.

Implementationsยง

sourceยง

impl Semaphore

source

pub fn new(permits: usize) -> Arc<Self>

Returns a new Arc<Semaphore> with the limit of permits chosen by you.

Examples found in repository?
examples/main.rs (line 7)
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
fn main() {
    let semaphore = simple_semaphore::Semaphore::new(2);

    for _ in 0..5 {
        let semaphore = Arc::clone(&semaphore);
        thread::spawn(move || {
            let permit = semaphore.acquire();
            thread::sleep(Duration::from_millis(500));
            drop(permit);
        });
    }
    thread::sleep(Duration::from_millis(3000));

    let cores = num_cpus::get();

    let semaphore = simple_semaphore::Semaphore::default(); // Also uses `num_cpus::get()` internally
    for _ in 0..(cores + 2) {
        let semaphore = Arc::clone(&semaphore);
        thread::spawn(move || {
            if let Some(permit) = semaphore.try_acquire() {
                thread::sleep(Duration::from_millis(500));
                drop(permit);
            } else {
                println!("Too many permits given, exiting the thread"); // Will be printed twice
            }
        });
    }
    thread::sleep(Duration::from_millis((((cores + 1) * 1000) / 2) as u64));
}
source

pub fn default() -> Arc<Self>

Returns a new Arc<Semaphore> with the limit of permits set to the number of CPU cores the computer has.

Examples found in repository?
examples/main.rs (line 21)
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
fn main() {
    let semaphore = simple_semaphore::Semaphore::new(2);

    for _ in 0..5 {
        let semaphore = Arc::clone(&semaphore);
        thread::spawn(move || {
            let permit = semaphore.acquire();
            thread::sleep(Duration::from_millis(500));
            drop(permit);
        });
    }
    thread::sleep(Duration::from_millis(3000));

    let cores = num_cpus::get();

    let semaphore = simple_semaphore::Semaphore::default(); // Also uses `num_cpus::get()` internally
    for _ in 0..(cores + 2) {
        let semaphore = Arc::clone(&semaphore);
        thread::spawn(move || {
            if let Some(permit) = semaphore.try_acquire() {
                thread::sleep(Duration::from_millis(500));
                drop(permit);
            } else {
                println!("Too many permits given, exiting the thread"); // Will be printed twice
            }
        });
    }
    thread::sleep(Duration::from_millis((((cores + 1) * 1000) / 2) as u64));
}
source

pub fn acquire(self: &Arc<Self>) -> Permit

Tries to get a Permit. If no more permits can be given, it will wait for one permit to be given back from a thread so that it can return a new Permit.

Examples found in repository?
examples/main.rs (line 12)
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
fn main() {
    let semaphore = simple_semaphore::Semaphore::new(2);

    for _ in 0..5 {
        let semaphore = Arc::clone(&semaphore);
        thread::spawn(move || {
            let permit = semaphore.acquire();
            thread::sleep(Duration::from_millis(500));
            drop(permit);
        });
    }
    thread::sleep(Duration::from_millis(3000));

    let cores = num_cpus::get();

    let semaphore = simple_semaphore::Semaphore::default(); // Also uses `num_cpus::get()` internally
    for _ in 0..(cores + 2) {
        let semaphore = Arc::clone(&semaphore);
        thread::spawn(move || {
            if let Some(permit) = semaphore.try_acquire() {
                thread::sleep(Duration::from_millis(500));
                drop(permit);
            } else {
                println!("Too many permits given, exiting the thread"); // Will be printed twice
            }
        });
    }
    thread::sleep(Duration::from_millis((((cores + 1) * 1000) / 2) as u64));
}
source

pub fn try_acquire(self: &Arc<Self>) -> Option<Permit>

Tries to get a Option<Permit>. If no more permits can be given, it will return None.

Examples found in repository?
examples/main.rs (line 25)
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
fn main() {
    let semaphore = simple_semaphore::Semaphore::new(2);

    for _ in 0..5 {
        let semaphore = Arc::clone(&semaphore);
        thread::spawn(move || {
            let permit = semaphore.acquire();
            thread::sleep(Duration::from_millis(500));
            drop(permit);
        });
    }
    thread::sleep(Duration::from_millis(3000));

    let cores = num_cpus::get();

    let semaphore = simple_semaphore::Semaphore::default(); // Also uses `num_cpus::get()` internally
    for _ in 0..(cores + 2) {
        let semaphore = Arc::clone(&semaphore);
        thread::spawn(move || {
            if let Some(permit) = semaphore.try_acquire() {
                thread::sleep(Duration::from_millis(500));
                drop(permit);
            } else {
                println!("Too many permits given, exiting the thread"); // Will be printed twice
            }
        });
    }
    thread::sleep(Duration::from_millis((((cores + 1) * 1000) / 2) as u64));
}
source

pub fn release(&self)

Releases a permit. This is what drop() on Permit calls, ideally use drop(permit).

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.