Struct grafix_toolbox::asyn::Barrier
source · pub struct Barrier { /* private fields */ }Expand description
A counter to synchronize multiple tasks at the same time.
Implementations§
source§impl Barrier
impl Barrier
sourcepub fn wait(&self) -> BarrierWait<'_> ⓘ
pub fn wait(&self) -> BarrierWait<'_> ⓘ
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");
});
});
}sourcepub fn wait_blocking(&self) -> BarrierWaitResult
pub fn wait_blocking(&self) -> BarrierWaitResult
Blocks the current thread 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.
§Blocking
Rather than using asynchronous waiting, like the wait method,
this method will block the current thread until the wait is complete.
This method should not be used in an asynchronous context. It is intended to be used in a way that a barrier can be used in both asynchronous and synchronous contexts. Calling this method in an asynchronous context may result in a deadlock.
§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 || {
// The same messages will be printed together.
// There will NOT be interleaving of "before" and "after".
println!("before wait");
b.wait_blocking();
println!("after wait");
});
}Trait Implementations§
Auto Trait Implementations§
impl !Freeze for Barrier
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
source§impl<T> Instrument for T
impl<T> Instrument for T
source§fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
source§fn in_current_span(self) -> Instrumented<Self> ⓘ
fn in_current_span(self) -> Instrumented<Self> ⓘ
source§impl<SS, SP> SupersetOf<SS> for SPwhere
SS: SubsetOf<SP>,
impl<SS, SP> SupersetOf<SS> for SPwhere
SS: SubsetOf<SP>,
source§fn to_subset(&self) -> Option<SS>
fn to_subset(&self) -> Option<SS>
self from the equivalent element of its
superset. Read moresource§fn is_in_subset(&self) -> bool
fn is_in_subset(&self) -> bool
self is actually part of its subset T (and can be converted to it).source§fn to_subset_unchecked(&self) -> SS
fn to_subset_unchecked(&self) -> SS
self.to_subset but without any property checks. Always succeeds.source§fn from_subset(element: &SS) -> SP
fn from_subset(element: &SS) -> SP
self to the equivalent element of its superset.