#waitgroup #async #sync #notify #wake

wg

Golang like WaitGroup implementation for sync/async Rust

7 unstable releases

new 0.4.2 Sep 30, 2023
0.4.1 Sep 30, 2023
0.3.2 May 24, 2023
0.3.1 Oct 1, 2022
0.1.1 Oct 25, 2021

#1 in #waitgroup

Download history 1219/week @ 2023-06-11 1750/week @ 2023-06-18 1663/week @ 2023-06-25 1831/week @ 2023-07-02 2142/week @ 2023-07-09 2091/week @ 2023-07-16 2455/week @ 2023-07-23 2390/week @ 2023-07-30 1918/week @ 2023-08-06 1867/week @ 2023-08-13 2302/week @ 2023-08-20 1866/week @ 2023-08-27 1421/week @ 2023-09-03 1613/week @ 2023-09-10 1842/week @ 2023-09-17 1797/week @ 2023-09-24

6,828 downloads per month
Used in 15 crates (2 directly)

MIT/Apache

44KB
450 lines

wg

Golang like WaitGroup implementation for sync/async Rust.

github Build codecov

docs.rs crates.io crates.io

license

Installation

[dependencies]
wg = "0.4"

Example

Sync

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);
}

Async

use wg::AsyncWaitGroup;
use std::sync::Arc;
use std::sync::atomic::{AtomicUsize, Ordering};
use tokio::{spawn, time::{sleep, Duration}};

#[tokio::main(flavor = "multi_thread", worker_threads = 10)]
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);
}

Acknowledgements

License

Licensed under either of Apache License, Version 2.0 or MIT license at your option.
Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in this project by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.

Dependencies

~0–5.5MB