Skip to main content

WaitGroup

Struct WaitGroup 

Source
pub struct WaitGroup { /* private fields */ }
Expand description

A synchronisation barrier: wait for a set of goroutines to complete.

The typical usage pattern is:

use std::sync::Arc;
use go_lib::sync::WaitGroup;

let wg = Arc::new(WaitGroup::new());
for i in 0..5 {
    let wg = Arc::clone(&wg);
    go_lib::run(move || {
        wg.add(1);
        // ... spawn goroutine that calls wg.done() when finished ...
    });
}
// wg.wait();  // blocks until all Done() calls have been made

Implementations§

Source§

impl WaitGroup

Source

pub fn new() -> Self

Create a new WaitGroup with a counter of zero.

Source

pub fn add(&self, delta: i64)

Add delta to the counter.

delta is typically positive when called before spawning goroutines and negative when they finish (see done).

§Panics

Panics if the counter drops below zero.

Source

pub fn done(&self)

Decrement the counter by one.

Shorthand for self.add(-1).

Source

pub fn wait(&self)

Block until the counter is zero.

When called from a goroutine: suspends the goroutine back into the scheduler via gopark so the M and P remain free to run other goroutines. Resumed by add calling goready when the counter reaches zero.

When called from a bare OS thread (outside the go-lib scheduler): blocks the thread on an internal Condvar.

Trait Implementations§

Source§

impl Default for WaitGroup

Source§

fn default() -> Self

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

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, 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, U> TryFrom<U> for T
where U: Into<T>,

Source§

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>,

Source§

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.