Struct Barrier

Source
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

Source

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 nth 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);
Source

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();
}

Trait Implementations§

Source§

impl Debug for Barrier

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.