logo
pub struct Barrier { /* private fields */ }
Expand description

A counter to synchronize multiple tasks at the same time.

Implementations

Creates a barrier that can block the given number of tasks.

A barrier will block n-1 tasks which call wait() and then wake up all tasks at once when the nth task calls wait().

Examples
use async_lock::Barrier;

let barrier = Barrier::new(5);

Blocks the current task until all tasks reach this point.

Barriers are reusable after all tasks have synchronized, and can be used continuously.

Returns a BarrierWaitResult indicating whether this task is the “leader”, meaning the last task to call this method.

Examples
use async_lock::Barrier;
use futures_lite::future;
use std::sync::Arc;
use std::thread;

let barrier = Arc::new(Barrier::new(5));

for _ in 0..5 {
    let b = barrier.clone();
    thread::spawn(move || {
        future::block_on(async {
            // The same messages will be printed together.
            // There will NOT be interleaving of "before" and "after".
            println!("before wait");
            b.wait().await;
            println!("after wait");
        });
    });
}

Trait Implementations

Formats the value using the given formatter. Read more

Auto Trait Implementations

Blanket Implementations

Gets the TypeId of self. Read more

Immutably borrows from an owned value. Read more

Mutably borrows from an owned value. Read more

Returns the argument unchanged.

Calls U::from(self).

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

The type returned in the event of a conversion error.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.