Crates.io | wg |
lib.rs | wg |
version | 0.9.1 |
source | src |
created_at | 2021-10-24 17:26:33.058805 |
updated_at | 2024-04-27 14:07:44.790438 |
description | Golang like WaitGroup implementation for sync/async Rust. |
homepage | https://github.com/al8n/wg |
repository | https://github.com/al8n/wg.git |
max_upload_size | |
id | 470467 |
size | 94,866 |
By default, blocking version WaitGroup
is enabled.
If you are using other async runtime, you need to
enbale future
feature in your Cargo.toml
and use wg::AsyncWaitGroup
.
std
[dependencies]
wg = "0.9"
future
[dependencies]
wg = { version = "0.9", features = ["future"] }
no_std
[dependencies]
wg = { version = "0.9", default_features = false, features = ["alloc"] }
no_std & future
[dependencies]
wg = { version = "0.9", default_features = false, features = ["alloc", "future"] }
use wg::WaitGroup;
use std::sync::Arc;
use std::sync::atomic::{AtomicUsize, Ordering};
use std::time::Duration;
use std::thread::{spawn, sleep};
fn main() {
let wg = WaitGroup::new();
let ctr = Arc::new(AtomicUsize::new(0));
for _ in 0..5 {
let ctrx = ctr.clone();
let t_wg = wg.add(1);
spawn(move || {
// mock some time consuming task
sleep(Duration::from_millis(50));
ctrx.fetch_add(1, Ordering::Relaxed);
// mock task is finished
t_wg.done();
});
}
wg.wait();
assert_eq!(ctr.load(Ordering::Relaxed), 5);
}
use wg::AsyncWaitGroup;
use std::sync::Arc;
use std::sync::atomic::{AtomicUsize, Ordering};
use tokio::{spawn, time::{sleep, Duration}};
#[tokio::main]
async fn main() {
let wg = AsyncWaitGroup::new();
let ctr = Arc::new(AtomicUsize::new(0));
for _ in 0..5 {
let ctrx = ctr.clone();
let t_wg = wg.add(1);
spawn(async move {
// mock some time consuming task
sleep(Duration::from_millis(50)).await;
ctrx.fetch_add(1, Ordering::Relaxed);
// mock task is finished
t_wg.done();
});
}
wg.wait().await;
assert_eq!(ctr.load(Ordering::Relaxed), 5);
}
crossbeam_utils::WaitGroup
.