[][src]Struct spin_sync::Barrier

pub struct Barrier { /* fields omitted */ }

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

impl Barrier[src]

pub const fn new(n: usize) -> Self[src]

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

pub fn wait(&self) -> BarrierWaitResult[src]

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

impl Debug for Barrier[src]

Auto Trait Implementations

impl !RefUnwindSafe for Barrier

impl Send for Barrier

impl Sync for Barrier

impl Unpin for Barrier

impl UnwindSafe for Barrier

Blanket Implementations

impl<T> Any for T where
    T: 'static + ?Sized
[src]

impl<T> Borrow<T> for T where
    T: ?Sized
[src]

impl<T> BorrowMut<T> for T where
    T: ?Sized
[src]

impl<T> From<T> for T[src]

impl<T, U> Into<U> for T where
    U: From<T>, 
[src]

impl<T, U> TryFrom<U> for T where
    U: Into<T>, 
[src]

type Error = Infallible

The type returned in the event of a conversion error.

impl<T, U> TryInto<U> for T where
    U: TryFrom<T>, 
[src]

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

The type returned in the event of a conversion error.