Struct adaptive_barrier::Barrier[][src]

pub struct Barrier(_);

A Barrier to synchronize multiple threads.

Multiple threads can meet on a single barrier to synchronize a "meeting point" in a computation (eg. when they need to pass results to others), much like the Barrier from the standard library.

Unlike that, the expected number of threads waiting for the barrier is not preset in the new call, but autodetected and adapted to at runtime.

The way this is done is by cloning the original Barrier ‒ for a group to continue after wait, a wait needs to be called on each clone. This allows to add or remove (even implicitly by panicking) the clones as needed.

Examples


let barrier = Barrier::new(PanicMode::Poison);
let mut threads = Vec::new();
for _ in 0..4 {
    // Each thread gets its own clone of the barrier. They are tied together, not independent.
    let mut barrier = barrier.clone();
    let thread = thread::spawn(move || {
        // Wait to start everything at the same time
        barrier.wait();

        // ... Do some work that needs to start synchronously ...
        // Now, if this part panics, it will *not* deadlock, it'll unlock the others just fine
        // and propagate the panic (see the parameter to new(..)

        // Wait for all threads to finish
        if barrier.wait().is_leader() {
            // Pick one thread to consolidate the results here

            // Note that as we don't call wait any more, if we panic here, it'll not get
            // propagated through the barrier any more.
        }
    });
    threads.push(thread);
}

// Watch out for the last instance here in the main/controlling thread. You can either call
// wait on it too, or make sure it is dropped. If you don't, others will keep waiting for it.
drop(barrier);

for thread in threads {
    thread.join().expect("Propagating thread panic");
}

Implementations

impl Barrier[src]

pub fn new(panic_mode: PanicMode) -> Self[src]

Creates a new (independent) barrier.

To create more handles to the same barrier, clone it.

The panic mode specifies what to do if a barrier observes a panic (is dropped while panicking).

pub fn wait(&mut self) -> WaitResult[src]

Wait for all the other threads to wait too.

This'll block until all threads holding clones of the same barrier call wait.

Panics

If the barrier was created with PanicMode::Poison and some other clone of the barrier observed a panic, this'll also panic (even if it was already parked inside).

Trait Implementations

impl Clone for Barrier[src]

impl Debug for Barrier[src]

impl Default for Barrier[src]

impl Drop for Barrier[src]

impl UnwindSafe for Barrier[src]

Auto Trait Implementations

Blanket Implementations

impl<T> Any for T where
    T: 'static + ?Sized
[src]

impl<T> Borrow<T> for T where
    T: ?Sized
[src]

impl<T> BorrowMut<T> for T where
    T: ?Sized
[src]

impl<T> From<T> for T[src]

impl<T, U> Into<U> for T where
    U: From<T>, 
[src]

impl<T> ToOwned for T where
    T: Clone
[src]

type Owned = T

The resulting type after obtaining ownership.

impl<T, U> TryFrom<U> for T where
    U: Into<T>, 
[src]

type Error = Infallible

The type returned in the event of a conversion error.

impl<T, U> TryInto<U> for T where
    U: TryFrom<T>, 
[src]

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

The type returned in the event of a conversion error.