Struct chan::WaitGroup [] [src]

pub struct WaitGroup(_);

WaitGroup provides synchronization on the completion of threads.

For each thread involved in the synchronization, add(1) should be called. Just before a thread terminates, it should call done. To synchronize, call wait, which will block until the number of done calls corresponds to the number of add(1) calls.

Example

use std::thread;

let wg = chan::WaitGroup::new();

for _ in 0..4 {
    wg.add(1);
    let wg = wg.clone();
    thread::spawn(move || {
        // do some work.

        // And now call done.
        wg.done();
    });
}
// Blocks until `wg.done()` is called for each thread we spawned.
wg.wait()

Methods

impl WaitGroup

fn new() -> WaitGroup

Create a new wait group.

fn add(&self, delta: i32)

Add a new thread to the waitgroup.

Failure

If the internal count drops below 0 as a result of calling add, then this function panics.

fn done(&self)

Mark a thread as having finished.

(This is equivalent to calling add(-1).)

fn wait(&self)

Wait until all threads have completed.

This unblocks when the internal count is 0.

Trait Implementations

impl Debug for WaitGroup

fn fmt(&self, f: &mut Formatter) -> Result

Derived Implementations

impl Clone for WaitGroup

fn clone(&self) -> WaitGroup

fn clone_from(&mut self, source: &Self)