Struct std::sync::Barrier
[−]
[src]
pub struct Barrier { // some fields omitted }
A barrier enables multiple threads to synchronize the beginning of some computation.
fn main() { use std::sync::{Arc, Barrier}; use std::thread; let barrier = Arc::new(Barrier::new(10)); for _ in 0..10 { let c = barrier.clone(); // The same messages will be printed together. // You will NOT see any interleaving. thread::spawn(move|| { println!("before wait"); c.wait(); println!("after wait"); }); } }use std::sync::{Arc, Barrier}; use std::thread; let barrier = Arc::new(Barrier::new(10)); for _ in 0..10 { let c = barrier.clone(); // The same messages will be printed together. // You will NOT see any interleaving. thread::spawn(move|| { println!("before wait"); c.wait(); println!("after wait"); }); }
Methods
impl Barrier
fn new(n: usize) -> Barrier
Creates a new barrier that can block a given number of threads.
A barrier will block n
`n-1 threads which call
`-1 threads which call wait
`waitand then wake up all threads at once when the
` and then wake up
all threads at once when the n
`nth thread calls
`th thread calls wait
`wait`.
fn wait(&self) -> BarrierWaitResult
Blocks the current thread until all threads has rendezvoused here.
Barriers are re-usable after all threads have rendezvoused once, and can be used continuously.
A single (arbitrary) thread will receive a BarrierWaitResult
`BarrierWaitResultthat returns
` that
returns true
`truefrom
` from is_leader
`is_leaderwhen returning from this function, and all other threads will receive a result that will return
` when returning from this function, and
all other threads will receive a result that will return false
`falsefrom
` from
is_leader
`is_leader`