pub struct AsyncWaitGroup { /* private fields */ }
Expand description

An AsyncWaitGroup waits for a collection of threads to finish. The main thread calls add to set the number of thread to wait for. Then each of the goroutines runs and calls Done when finished. At the same time, Wait can be used to block until all goroutines have finished.

A WaitGroup must not be copied after first use.

Example

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

Implementations

Creates a new AsyncWaitGroup

Adds delta to the WaitGroup counter. If the counter becomes zero, all threads blocked on wait are released.

Note that calls with a delta that occur when the counter is zero must happen before a Wait. Typically this means the calls to add should execute before the statement creating the thread or other event to be waited for. If a AsyncWaitGroup is reused to wait for several independent sets of events, new add calls must happen after all previous wait calls have returned.

Example
use wg::AsyncWaitGroup;

#[tokio::main(flavor = "multi_thread", worker_threads = 10)]
async fn main() {
    let wg = AsyncWaitGroup::new();

    wg.add(3);
    (0..3).for_each(|_| {
        let t_wg = wg.clone();
        tokio::spawn(async move {
            // do some time consuming work
            t_wg.done();
        });
    });

    wg.wait().await;
}

done decrements the WaitGroup counter by one.

Example
use wg::AsyncWaitGroup;

#[tokio::main(flavor = "multi_thread", worker_threads = 10)]
async fn main() {
    let wg = AsyncWaitGroup::new();
    wg.add(1);
    let t_wg = wg.clone();
    tokio::spawn(async move {
        // do some time consuming task
        t_wg.done();
    });
}

waitings return how many jobs are waiting.

wait blocks until the WaitGroup counter is zero.

Example
use wg::AsyncWaitGroup;

#[tokio::main(flavor = "multi_thread", worker_threads = 10)]
async fn main() {
    let wg = AsyncWaitGroup::new();
    wg.add(1);
    let t_wg = wg.clone();

    tokio::spawn( async move {
        // do some time consuming task
        t_wg.done()
    });

    // wait other thread completes
    wg.wait().await;
}

Trait Implementations

Returns a copy of the value. Read more
Performs copy-assignment from source. Read more
Formats the value using the given formatter. Read more
Returns the “default value” for a type. Read more
Converts to this type from the input type.

Auto Trait Implementations

Blanket Implementations

Gets the TypeId of self. Read more
Immutably borrows from an owned value. Read more
Mutably borrows from an owned value. Read more

Returns the argument unchanged.

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

The resulting type after obtaining ownership.
Creates owned data from borrowed data, usually by cloning. Read more
Uses borrowed data to replace owned data, usually by cloning. Read more
The type returned in the event of a conversion error.
Performs the conversion.
The type returned in the event of a conversion error.
Performs the conversion.