pub struct CyclicBarrier { /* private fields */ }
Expand description
Cyclic Barrier implementation.
It basically provides 2 methods:
let barrier = CyclicBarrier::new(count); // initialize a cyclic barrier with 3 parties barrier.wait(); // Wait until all parties are wait. Returnes Ok(()) for the last one entered the state
Most of the time, you should use Arc(barrier).
You can clone the Arc container of barrier as many times as you want. // You can reuse the barrier if the config does not need to be changed.
drop(barrier); // discard the barrier after you are done with it.
Usage: Example:
use std::sync::Arc;
use std::time::Duration;
use std::thread;
use classic_sync::cyclic_barrier::CyclicBarrier;
let barrier = CyclicBarrier::new(3);
let arc_barrier = Arc::new(barrier);
for i in 0..3 {
let barrier_copy = Arc::clone(&arc_barrier);
let tid = i;
thread::spawn(move || {
// if you care who's wait call triggered everyone to go, you can check the
// firing object returned. If it is Some(_), it triggered the barrier
let firing = barrier_copy.wait();
if firing.is_some() {
println!("Thread {tid} is the one triggered it!");
}
println!("Now we are starting almost at the same time!!");
});
}
Implementations§
Source§impl CyclicBarrier
Implementation of CyclicBarrier
impl CyclicBarrier
Implementation of CyclicBarrier
Sourcepub fn new(count: u32) -> CyclicBarrier
pub fn new(count: u32) -> CyclicBarrier
Create a new CyclicBarrier with count of count
. It is backed by a C pthread_barrier_t object.
count must be greater than 0. If not behavior is not determined.
Trait Implementations§
Auto Trait Implementations§
impl Freeze for CyclicBarrier
impl RefUnwindSafe for CyclicBarrier
impl Send for CyclicBarrier
impl Sync for CyclicBarrier
impl Unpin for CyclicBarrier
impl UnwindSafe for CyclicBarrier
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