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 aPermit
. 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 newPermit
. - When
try_acquire()
is called and the semaphore still has permits to give, it will returnSome(Permit)
. If there are no permits that can be given, it will returnNone
.
Implementationsยง
Sourceยงimpl Semaphore
impl Semaphore
Sourcepub fn new(permits: usize) -> Arc<Self>
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)
6fn main() {
7 let semaphore = simple_semaphore::Semaphore::new(2);
8
9 for _ in 0..5 {
10 let semaphore = Arc::clone(&semaphore);
11 thread::spawn(move || {
12 let permit = semaphore.acquire();
13 thread::sleep(Duration::from_millis(500));
14 drop(permit);
15 });
16 }
17 thread::sleep(Duration::from_millis(3000));
18
19 let cores = num_cpus::get();
20
21 let semaphore = simple_semaphore::Semaphore::default(); // Also uses `num_cpus::get()` internally
22 for _ in 0..(cores + 2) {
23 let semaphore = Arc::clone(&semaphore);
24 thread::spawn(move || {
25 if let Some(permit) = semaphore.try_acquire() {
26 thread::sleep(Duration::from_millis(500));
27 drop(permit);
28 } else {
29 println!("Too many permits given, exiting the thread"); // Will be printed twice
30 }
31 });
32 }
33 thread::sleep(Duration::from_millis((((cores + 1) * 1000) / 2) as u64));
34}
Sourcepub fn default() -> Arc<Self>
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)
6fn main() {
7 let semaphore = simple_semaphore::Semaphore::new(2);
8
9 for _ in 0..5 {
10 let semaphore = Arc::clone(&semaphore);
11 thread::spawn(move || {
12 let permit = semaphore.acquire();
13 thread::sleep(Duration::from_millis(500));
14 drop(permit);
15 });
16 }
17 thread::sleep(Duration::from_millis(3000));
18
19 let cores = num_cpus::get();
20
21 let semaphore = simple_semaphore::Semaphore::default(); // Also uses `num_cpus::get()` internally
22 for _ in 0..(cores + 2) {
23 let semaphore = Arc::clone(&semaphore);
24 thread::spawn(move || {
25 if let Some(permit) = semaphore.try_acquire() {
26 thread::sleep(Duration::from_millis(500));
27 drop(permit);
28 } else {
29 println!("Too many permits given, exiting the thread"); // Will be printed twice
30 }
31 });
32 }
33 thread::sleep(Duration::from_millis((((cores + 1) * 1000) / 2) as u64));
34}
Sourcepub fn available_permits(self: &Arc<Self>) -> usize
pub fn available_permits(self: &Arc<Self>) -> usize
Returns the number of available permits
Sourcepub fn acquire(self: &Arc<Self>) -> Permit
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)
6fn main() {
7 let semaphore = simple_semaphore::Semaphore::new(2);
8
9 for _ in 0..5 {
10 let semaphore = Arc::clone(&semaphore);
11 thread::spawn(move || {
12 let permit = semaphore.acquire();
13 thread::sleep(Duration::from_millis(500));
14 drop(permit);
15 });
16 }
17 thread::sleep(Duration::from_millis(3000));
18
19 let cores = num_cpus::get();
20
21 let semaphore = simple_semaphore::Semaphore::default(); // Also uses `num_cpus::get()` internally
22 for _ in 0..(cores + 2) {
23 let semaphore = Arc::clone(&semaphore);
24 thread::spawn(move || {
25 if let Some(permit) = semaphore.try_acquire() {
26 thread::sleep(Duration::from_millis(500));
27 drop(permit);
28 } else {
29 println!("Too many permits given, exiting the thread"); // Will be printed twice
30 }
31 });
32 }
33 thread::sleep(Duration::from_millis((((cores + 1) * 1000) / 2) as u64));
34}
Sourcepub fn try_acquire(self: &Arc<Self>) -> Option<Permit>
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)
6fn main() {
7 let semaphore = simple_semaphore::Semaphore::new(2);
8
9 for _ in 0..5 {
10 let semaphore = Arc::clone(&semaphore);
11 thread::spawn(move || {
12 let permit = semaphore.acquire();
13 thread::sleep(Duration::from_millis(500));
14 drop(permit);
15 });
16 }
17 thread::sleep(Duration::from_millis(3000));
18
19 let cores = num_cpus::get();
20
21 let semaphore = simple_semaphore::Semaphore::default(); // Also uses `num_cpus::get()` internally
22 for _ in 0..(cores + 2) {
23 let semaphore = Arc::clone(&semaphore);
24 thread::spawn(move || {
25 if let Some(permit) = semaphore.try_acquire() {
26 thread::sleep(Duration::from_millis(500));
27 drop(permit);
28 } else {
29 println!("Too many permits given, exiting the thread"); // Will be printed twice
30 }
31 });
32 }
33 thread::sleep(Duration::from_millis((((cores + 1) * 1000) / 2) as u64));
34}
Trait Implementationsยง
Auto Trait Implementationsยง
impl !Freeze for Semaphore
impl RefUnwindSafe for Semaphore
impl Send for Semaphore
impl Sync for Semaphore
impl Unpin for Semaphore
impl UnwindSafe for Semaphore
Blanket Implementationsยง
Sourceยงimpl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Sourceยงfn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more