pub struct Latch { /* private fields */ }Expand description
A synchronization primitive that can be used to coordinate multiple tasks.
A latch starts with an initial count and tasks can wait for this count to reach zero.
The count can be decremented by calling count_down() or arrive(). Once the count
reaches zero, all waiting tasks are unblocked.
§Examples
use std::sync::Arc;
use mea::latch::Latch;
let latch = Arc::new(Latch::new(3));
let mut handles = Vec::new();
// Spawn tasks that will count down the latch
for i in 0..3 {
let latch = latch.clone();
handles.push(tokio::spawn(async move {
// Do some work
println!("Task {} completing", i);
latch.count_down();
}));
}
// Wait for all tasks to complete
latch.wait().await;
println!("All tasks completed!");
for handle in handles {
handle.await.unwrap();
}Implementations§
Source§impl Latch
impl Latch
Sourcepub fn count(&self) -> u32
pub fn count(&self) -> u32
Returns the current count.
This method is typically used for debugging and testing purposes.
§Examples
use mea::latch::Latch;
let latch = Latch::new(5);
assert_eq!(latch.count(), 5);Sourcepub fn count_down(&self)
pub fn count_down(&self)
Decrements the latch count by one, waking up all pending tasks if the counter reaches zero.
If the current count is zero, this method has no effect.
§Examples
use mea::latch::Latch;
let latch = Latch::new(2);
latch.count_down(); // Count is now 1
latch.count_down(); // Count is now 0, all waiting tasks are wokenSourcepub fn arrive(&self, n: u32)
pub fn arrive(&self, n: u32)
Decrements the latch count by n, waking up all waiting tasks if the counter reaches zero.
This method provides a way to decrement the counter by more than one at a time. It will not cause an overflow when decrementing the counter.
§Arguments
n- The amount to decrement the counter by
§Behavior
- If
nis zero or the counter has already reached zero, nothing happens - If the current count is greater than
n, it is decremented byn - If the current count is greater than 0 but less than or equal to
n, the count becomes zero and all waiting tasks are woken
§Examples
use mea::latch::Latch;
let latch = Latch::new(5);
latch.arrive(3); // Count is now 2
latch.arrive(2); // Count is now 0, all waiting tasks are wokenSourcepub fn try_wait(&self) -> Result<(), u32>
pub fn try_wait(&self) -> Result<(), u32>
Attempts to wait for the latch count to reach zero without blocking.
§Returns
Ok(())if the count is zeroErr(count)if the count is not zero, wherecountis the current count
§Examples
use mea::latch::Latch;
let latch = Latch::new(2);
assert_eq!(latch.try_wait(), Err(2));
latch.count_down();
assert_eq!(latch.try_wait(), Err(1));
latch.count_down();
assert_eq!(latch.try_wait(), Ok(()));Sourcepub fn wait(&self) -> LatchWait<'_> ⓘ
pub fn wait(&self) -> LatchWait<'_> ⓘ
Returns a future that will complete when the latch count reaches zero.
§Examples
use std::sync::Arc;
use mea::latch::Latch;
let latch = Arc::new(Latch::new(1));
let latch2 = latch.clone();
// Spawn a task that will wait for the latch
let handle = tokio::spawn(async move {
latch2.wait().await;
println!("Latch reached zero!");
});
// Count down the latch
latch.count_down();
handle.await.unwrap();