Crate wait_for_me
source ·Expand description
This library provides an implementation of an async CountDownLatch
,
which keeps a counter synchronized via [Lock
][async-lock::Lock] in it’s internal state and allows tasks to wait until
the counter reaches zero.
Example
use wait_for_me::CountDownLatch;
use smol::{self,Task};
fn main() -> Result<(), Box<dyn std::error::Error>> {
smol::block_on(async {
let latch = CountDownLatch::new(1);
let latch1 = latch.clone();
smol::spawn(async move {
latch1.count_down().await;
}).detach();
latch.wait().await;
Ok(())
})
}
With timeout
use wait_for_me::CountDownLatch;
use smol::{Task,Timer};
use std::time::Duration;
#[smol_potat::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
let latch = CountDownLatch::new(10);
for _ in 0..10 {
let latch1 = latch.clone();
smol::spawn(async move {
Timer::after(Duration::from_secs(3)).await;
latch1.count_down().await;
}).detach();
}
let result = latch.wait_for(Duration::from_secs(1)).await;
assert_eq!(false,result);
Ok(())
}
Structs
- A synchronization primitive that allows one or more tasks to wait until the given counter reaches zero. This is an async port of CountDownLatch in Java.