pub struct WaitGroup { /* private fields */ }Expand description
WaitGroup is a synchronization primitive that allows to wait
until all tasks are completed.
§The difference between WaitGroup and LocalWaitGroup
The WaitGroup works with shared tasks and can be shared between threads.
Read Executor for more details.
§Example
use std::time::Duration;
use std::sync::atomic::{AtomicUsize, Ordering::SeqCst};
use orengine::sleep;
use orengine::sync::{shared_scope, AsyncWaitGroup, WaitGroup};
let wait_group = WaitGroup::new();
let number_executed_tasks = AtomicUsize::new(0);
shared_scope(|scope| async {
for i in 0..10 {
wait_group.inc();
scope.spawn(async {
sleep(Duration::from_millis(i)).await;
number_executed_tasks.fetch_add(1, SeqCst);
wait_group.done();
});
}
wait_group.wait().await; // wait until all tasks are completed
assert_eq!(number_executed_tasks.load(SeqCst), 10);
}).await;Implementations§
Trait Implementations§
Source§impl AsyncWaitGroup for WaitGroup
impl AsyncWaitGroup for WaitGroup
impl RefUnwindSafe for WaitGroup
impl Send for WaitGroup
impl Sync for WaitGroup
impl UnwindSafe for WaitGroup
Auto Trait Implementations§
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
Mutably borrows from an owned value. Read more