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.


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

        // ... 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.

// 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.

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


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.


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

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

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

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

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

impl<T> ToOwned for T where
    T: Clone

type Owned = T

The resulting type after obtaining ownership.

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

type Error = Infallible

The type returned in the event of a conversion error.

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.