Crate atomic_waitgroup
source ·Expand description
A waitgroup support async with advanced features, implemented with atomic operations to reduce locking in mind.
§Features & restrictions
-
wait_to() is supported to wait for a value larger than zero.
-
wait() & wait_to() can be canceled by tokio::time::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.
-
add() & wait() will not conflict, but concurrent calls are not a good pattern.
§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");
}
}
});