pub struct CountdownEvent { /* private fields */ }
Expand description
A synchronization primitive that signals when its count reaches zero.
With a CountdownEvent
, it’s possible to cause one thread to wait on a set of computations
occurring in other threads by making the other threads interact with the counter as they
perform their work.
The main limitation of a CountdownEvent is that once its counter reaches zero (even by starting
there), any attempts to update the counter will return CountdownError::AlreadySet
until the
counter is reset by calling reset
or reset_to_count
.
CountdownEvent
is a port of System.Threading.CountdownEvent from .NET (also
called CountDownLatch
in Java).
§Example
This example uses a CountdownEvent
to make the “coordinator” thread sleep until all of its
“worker” threads have finished. Each thread calls signal.decrement()
to signal to the Event
that its work has completed. When the last thread does this (and brings the counter to zero),
the “coordinator” thread wakes up and prints all done!
.
use synchronoise::CountdownEvent;
use std::sync::Arc;
use std::thread;
use std::time::Duration;
let thread_count = 5;
let counter = Arc::new(CountdownEvent::new(thread_count));
for i in 0..thread_count {
let signal = counter.clone();
thread::spawn(move || {
thread::sleep(Duration::from_secs(i as u64));
println!("thread {} activated!", i);
signal.decrement().unwrap();
});
}
counter.wait();
println!("all done!");
Implementations§
Source§impl CountdownEvent
impl CountdownEvent
Sourcepub fn new(count: usize) -> CountdownEvent
pub fn new(count: usize) -> CountdownEvent
Creates a new CountdownEvent
, initialized to the given count.
Remember that once the counter reaches zero, calls to add
or signal
will fail, so
passing zero to this function will create a CountdownEvent
that is permanently signaled.
Sourcepub fn reset(&mut self)
pub fn reset(&mut self)
Resets the counter to the count given to new
.
This function is safe because the &mut self
enforces that no other references or locks
exist.
Sourcepub fn reset_to_count(&mut self, count: usize)
pub fn reset_to_count(&mut self, count: usize)
Resets the counter to the given count.
This function is safe because the &mut self
enforces that no other references or locks
exist.
Sourcepub fn add(&self, count: usize) -> Result<(), CountdownError>
pub fn add(&self, count: usize) -> Result<(), CountdownError>
Adds the given count to the counter.
§Errors
If the counter is already at zero, this function will return CountdownError::AlreadySet
.
If the given count would cause the counter to overflow usize
, this function will return
CountdownError::SaturatedCounter
.
Sourcepub fn signal(&self, count: usize) -> Result<bool, CountdownError>
pub fn signal(&self, count: usize) -> Result<bool, CountdownError>
Subtracts the given count to the counter, and returns whether this caused any waiting threads to wake up.
§Errors
If the counter is already at zero, this function will return CountdownError::AlreadySet
.
If the given count would cause the counter to go below zero (instead of reaching zero),
this function will return CountdownError::TooManySignals
.
Sourcepub fn increment(&self) -> Result<(), CountdownError>
pub fn increment(&self) -> Result<(), CountdownError>
Sourcepub fn decrement(&self) -> Result<bool, CountdownError>
pub fn decrement(&self) -> Result<bool, CountdownError>
Sourcepub fn guard(&self) -> Result<CountdownGuard<'_>, CountdownError>
pub fn guard(&self) -> Result<CountdownGuard<'_>, CountdownError>
Increments the counter, then returns a guard object that will decrement the counter upon drop.
§Errors
This function will return the same errors as add
. If the event has already signaled by
the time the guard is dropped (and would cause its decrement
call to return an error),
then the error will be silently ignored.
§Example
Here’s the sample from the main docs, using CountdownGuard
s instead of manually
decrementing:
use synchronoise::CountdownEvent;
use std::sync::Arc;
use std::thread;
use std::time::Duration;
let thread_count = 5;
// counter can't start from zero, but the guard increments on its own, so start at one and
// just decrement once when we're ready to wait
let counter = Arc::new(CountdownEvent::new(1));
for i in 0..thread_count {
let signal = counter.clone();
thread::spawn(move || {
let _guard = signal.guard().unwrap();
thread::sleep(Duration::from_secs(i));
println!("thread {} activated!", i);
});
}
// give all the threads time to increment the counter before continuing
thread::sleep(Duration::from_millis(100));
counter.decrement().unwrap();
counter.wait();
println!("all done!");
Sourcepub fn wait(&self)
pub fn wait(&self)
Blocks the current thread until the counter reaches zero.
This function will block indefinitely until the counter reaches zero. It will return immediately if it is already at zero.
Sourcepub fn wait_timeout(&self, timeout: Duration) -> usize
pub fn wait_timeout(&self, timeout: Duration) -> usize
Blocks the current thread until the timer reaches zero, or until the given timeout elapses, returning the count at the time of wakeup.
This function will return immediately if the counter was already at zero. Otherwise, it
will block for roughly no longer than timeout
, or when the counter reaches zero,
whichever comes first.
Auto Trait Implementations§
impl !Freeze for CountdownEvent
impl RefUnwindSafe for CountdownEvent
impl Send for CountdownEvent
impl Sync for CountdownEvent
impl Unpin for CountdownEvent
impl UnwindSafe for CountdownEvent
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
Layout§
Note: Most layout information is completely unstable and may even differ between compilations. The only exception is types with certain repr(...)
attributes. Please see the Rust Reference's “Type Layout” chapter for details on type layout guarantees.
Size: 384 bytes