pub struct Barrier { /* private fields */ }
Expand description
A counter to synchronize multiple tasks at the same time.
Implementations§
source§impl Barrier
impl Barrier
sourcepub async fn wait(&self) -> BarrierWaitResult
pub async fn wait(&self) -> BarrierWaitResult
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§
Auto Trait Implementations§
impl !RefUnwindSafe for Barrier
impl Send for Barrier
impl Sync for Barrier
impl Unpin for Barrier
impl UnwindSafe for Barrier
Blanket Implementations§
source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more