pub struct Barrier { /* private fields */ }
Expand description
A barrier enables multiple threads to synchronize the beginning of some computation.
The behavior is same to std::sync::Barrier
except for this uses spinlock.
Unlike to std::sync::Barrier::new
, the constructor Barrier.new
is a const function;
i.e. static Barrier
variable can be declared.
§Examples
use spin_sync::Barrier;
use std::thread;
static NUM_THREADS: usize = 10;
static BARRIER: Barrier = Barrier::new(NUM_THREADS);
let mut handles = Vec::with_capacity(10);
for _ in 0..10 {
// The same messages will be printed together.
// You will NOT see any interleaving.
handles.push(thread::spawn(move|| {
println!("before wait");
BARRIER.wait();
println!("after wait");
}));
}
// Wait for other threads to finish.
for handle in handles {
handle.join().unwrap();
}
Once all the threads have finished to wait, Barrier
is reinitialized.
The same instance can be used again.
use spin_sync::Barrier;
use std::thread;
static NUM_THREADS: usize = 10;
static BARRIER: Barrier = Barrier::new(NUM_THREADS);
fn wait_and_reinitialize() {
let mut handles = Vec::with_capacity(10);
for _ in 0..10 {
// The same messages will be printed together.
// You will NOT see any interleaving.
handles.push(thread::spawn(move|| {
println!("before wait");
BARRIER.wait();
println!("after wait");
}));
}
// Wait for other threads to finish.
for handle in handles {
handle.join().unwrap();
}
}
fn main() {
// First use.
wait_and_reinitialize();
// Second use.
wait_and_reinitialize();
}
If 0 or 1 is passed to Barrier::new
, the instance will never block.
use spin_sync::Barrier;
use std::thread;
static BARRIER0: Barrier = Barrier::new(0);
static BARRIER1: Barrier = Barrier::new(1);
BARRIER0.wait();
BARRIER1.wait();
Implementations§
Source§impl Barrier
impl Barrier
Sourcepub const fn new(n: usize) -> Self
pub const fn new(n: usize) -> Self
Creates a new barrier that can block a given number of threads.
Unlike to std::sync::Barrier::new
, this function is const; i.e.
static Barrier
variable can be declared.
A barrier will block n
-1 threads which call wait
and then wake up
all threads at once when the n
th thread calls wait
.
§Examples
Declaring Barrier
instance as a local variable.
use spin_sync::Barrier;
let barrier = Barrier::new(10);
Declaring static Barrier
variable.
use spin_sync::Barrier;
static BARRIER: Barrier = Barrier::new(5);
Sourcepub fn wait(&self) -> BarrierWaitResult
pub fn wait(&self) -> BarrierWaitResult
Blocks the current thread until all threads have 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
that
returns true
from is_leader
when returning from this function, and
all other threads will receive a result that will return false
from
is_leader
.
§Examples
use spin_sync::Barrier;
use std::thread;
static NUM_THREADS: usize = 10;
static BARRIER: Barrier = Barrier::new(NUM_THREADS);
let mut handles = Vec::with_capacity(10);
for _ in 0..10 {
// The same messages will be printed together.
// You will NOT see any interleaving.
handles.push(thread::spawn(move|| {
println!("before wait");
BARRIER.wait();
println!("after wait");
}));
}
// Wait for other threads to finish.
for handle in handles {
handle.join().unwrap();
}