pub struct Barrier(/* private fields */);
Expand description
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§
Source§impl Barrier
impl Barrier
Sourcepub fn new(panic_mode: PanicMode) -> Self
pub fn new(panic_mode: PanicMode) -> Self
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).
Sourcepub fn wait(&mut self) -> WaitResult
pub fn wait(&mut self) -> WaitResult
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 UnwindSafe for Barrier
Auto Trait Implementations§
impl Freeze for Barrier
impl RefUnwindSafe for Barrier
impl Send for Barrier
impl Sync for Barrier
impl Unpin 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> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§unsafe fn clone_to_uninit(&self, dst: *mut T)
unsafe fn clone_to_uninit(&self, dst: *mut T)
clone_to_uninit
)