Crate awaitgroup

source ·
Expand description

Documentation Version License Actions

A WaitGroup waits for a collection of async tasks to finish.

The main task can create new workers and pass them to each of the tasks it wants to wait for. Each of the tasks calls done when it finishes executing, and the main task can call wait to block until all registered workers are done.

Examples

use awaitgroup::WaitGroup;

let mut wg = WaitGroup::new();

for _ in 0..5 {
    // Create a new worker.
    let worker = wg.worker();

    tokio::spawn(async {
        // Do some work...

        // This task is done all of its work.
        worker.done();
    });
}

// Block until all other tasks have finished their work.
wg.wait().await;

A WaitGroup can be re-used and awaited multiple times.

let mut wg = WaitGroup::new();

let worker = wg.worker();

tokio::spawn(async {
    // Do work...
    worker.done();
});

// Wait for tasks to finish
wg.wait().await;

// Re-use wait group
let worker = wg.worker();

tokio::spawn(async {
    // Do more work...
    worker.done();
});

wg.wait().await;

Structs

  • Wait for a collection of tasks to finish execution.
  • A worker registered in a WaitGroup.