Struct wg::WaitGroup

source ·
pub struct WaitGroup { /* private fields */ }
Available on crate feature std only.
Expand description

A WaitGroup 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::WaitGroup;
use std::sync::Arc;
use std::sync::atomic::{AtomicUsize, Ordering};
use std::time::Duration;
use std::thread::{spawn, sleep};

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

Implementations§

source§

impl WaitGroup

source

pub fn new() -> Self

Creates a new wait group and returns the single reference to it.

§Examples
use wg::WaitGroup;

let wg = WaitGroup::new();
source

pub fn add(&self, num: usize) -> Self

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 WaitGroup 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::WaitGroup;

let wg = WaitGroup::new();

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

wg.wait();
source

pub fn done(&self)

done decrements the WaitGroup counter by one.

§Example
use wg::WaitGroup;
use std::thread;

let wg = WaitGroup::new();
wg.add(1);
let t_wg = wg.clone();
thread::spawn(move || {
    // do some time consuming task
    t_wg.done()
});
source

pub fn waitings(&self) -> usize

waitings return how many jobs are waiting.

source

pub fn wait(&self)

wait blocks until the WaitGroup counter is zero.

§Example
use wg::WaitGroup;
use std::thread;

let wg = WaitGroup::new();
wg.add(1);
let t_wg = wg.clone();
thread::spawn(move || {
    // do some time consuming task
    t_wg.done()
});

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

Trait Implementations§

source§

impl Clone for WaitGroup

source§

fn clone(&self) -> Self

Returns a copy of the value. Read more
1.0.0 · source§

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

Performs copy-assignment from source. Read more
source§

impl Debug for WaitGroup

source§

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

Formats the value using the given formatter. Read more
source§

impl Default for WaitGroup

source§

fn default() -> Self

Returns the “default value” for a type. Read more
source§

impl From<usize> for WaitGroup

source§

fn from(count: usize) -> Self

Converts to this type from the input type.

Auto Trait Implementations§

Blanket Implementations§

source§

impl<T> Any for T
where T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for T
where T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T> Instrument for T

source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
source§

impl<T, U> Into<U> for T
where U: From<T>,

source§

fn into(self) -> U

Calls U::from(self).

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

source§

impl<T> ToOwned for T
where T: Clone,

§

type Owned = T

The resulting type after obtaining ownership.
source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
source§

impl<T> WithSubscriber for T

source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more