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

no-std wg

Golang like WaitGroup implementation for sync/async Rust

5 releases (3 breaking)

new 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

#90 in Asynchronous

Download history 1532/week @ 2024-01-07 1161/week @ 2024-01-14 1358/week @ 2024-01-21 2175/week @ 2024-01-28 2020/week @ 2024-02-04 4025/week @ 2024-02-11 2949/week @ 2024-02-18 3061/week @ 2024-02-25 2163/week @ 2024-03-03 2598/week @ 2024-03-10 2198/week @ 2024-03-17 2087/week @ 2024-03-24 1805/week @ 2024-03-31 1135/week @ 2024-04-07 1928/week @ 2024-04-14 1278/week @ 2024-04-21

6,276 downloads per month
Used in 24 crates (3 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–6.5MB
~20K SLoC