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

no-std wg

Golang like WaitGroup implementation for sync/async Rust

5 releases (3 breaking)

0.9.1 Apr 27, 2024
0.8.3 Apr 20, 2024
0.8.1 Mar 13, 2024
0.4.2 Sep 30, 2023
0.2.0 Nov 30, 2021

#78 in Asynchronous

Download history 4452/week @ 2024-02-15 3031/week @ 2024-02-22 2141/week @ 2024-02-29 2853/week @ 2024-03-07 2075/week @ 2024-03-14 2035/week @ 2024-03-21 2109/week @ 2024-03-28 1328/week @ 2024-04-04 1514/week @ 2024-04-11 1659/week @ 2024-04-18 1958/week @ 2024-04-25 1918/week @ 2024-05-02 2420/week @ 2024-05-09 2578/week @ 2024-05-16 1874/week @ 2024-05-23 1514/week @ 2024-05-30

8,743 downloads per month
Used in 24 crates (5 directly)

MIT/Apache

34KB
362 lines

wg

Golang like WaitGroup implementation for sync/async Rust, support no_std environment.

github Build codecov

docs.rs crates.io crates.io

license

Introduction

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.

Installation

  • 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"] }
    

Examples

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]
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–6MB
~20K SLoC