Expand description
A waitgroup support async with advanced features
implemented with atomic operations to reduce locking.
§Features & restrictions
-
wait_to() supports waiting for a value >= zero
-
wait() & wait_to() can be canceled wrapped by timeout or futures::select!.
-
Assumes only one thread calls wait(). If multiple concurrent wait() is detected, will panic for this invalid usage.
-
done() & wait() is allowed to called concurrently.
-
add() & done() is allowed to called concurrently.
-
Assumes add() and wait() are in the same thread.
§Example
extern crate atomic_waitgroup;
use atomic_waitgroup::WaitGroup;
use tokio::runtime::Runtime;
let rt = Runtime::new().unwrap();
let wg = WaitGroup::new();
rt.block_on(async move {
for i in 0..2 {
let _guard = wg.add_guard();
tokio::spawn(async move {
// Do something
drop(_guard);
});
}
match tokio::time::timeout(
tokio::time::Duration::from_secs(1),
wg.wait_to(1)).await {
Ok(_) => {
assert!(wg.left() <= 1);
}
Err(_) => {
println!("wg.wait_to(1) timeouted");
}
}
});