#wait-group #sync #async #notify #tokio #wake

no-std wg

Golang like WaitGroup implementation for sync/async Rust

10 unstable releases (3 breaking)

new 0.8.3 Apr 20, 2024
0.8.1 Mar 13, 2024
0.4.2 Sep 30, 2023
0.3.2 May 24, 2023
0.2.0 Nov 30, 2021

#85 in Asynchronous

Download history 819/week @ 2024-01-01 1579/week @ 2024-01-08 1147/week @ 2024-01-15 1451/week @ 2024-01-22 2101/week @ 2024-01-29 2218/week @ 2024-02-05 4460/week @ 2024-02-12 3006/week @ 2024-02-19 2396/week @ 2024-02-26 2288/week @ 2024-03-04 2460/week @ 2024-03-11 2177/week @ 2024-03-18 2119/week @ 2024-03-25 1780/week @ 2024-04-01 1103/week @ 2024-04-08 1854/week @ 2024-04-15

6,966 downloads per month
Used in 28 crates (3 directly)

MIT/Apache

39KB
390 lines

wg

Golang like WaitGroup implementation for sync/async Rust.

github Build codecov

docs.rs crates.io crates.io

license

Introduction

By default, blocking version WaitGroup is enabled.

If you are using tokio, you need to enable tokio feature in your Cargo.toml and use wg::tokio::AsyncWaitGroup.

If you are using other async runtime, you need to enbale future feature in your Cargo.toml and use wg::future::AsyncWaitGroup.

Sync

[dependencies]
wg = "0.8"

tokio

An async implementation for tokio runtime.

[dependencies]
wg = { version = "0.8", features = ["tokio"] }

future

A more generic async implementation.

[dependencies]
wg = { version = "0.8", features = ["future"] }

Instruction

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

tokio

use wg::tokio::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);
}

async-io

use wg::future::AsyncWaitGroup;
use std::sync::Arc;
use std::sync::atomic::{AtomicUsize, Ordering};
use std::time::Duration;
use async_std::task::{spawn, block_on, sleep};

fn main() {
    block_on(async {
        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–14MB
~140K SLoC