Crate pool_barrier [] [src]

A barrier for blocking a main thread until the completion of work which has been offloaded to worker threads, without blocking the worker threads.

This barrier allows blocking on wait() until n Checkpoints have been cleared using check_in() or drop(). Threads which call check_in() do not block, in contrast to std::sync::Barrier which blocks all threads and potentially deadlocks when used with an over-utilised threadpool.

To use and reuse the Barrier an ActiveBarrier must be generated using activate(), which can then be used to generate checkpoints using checkpoint(). An ActiveBarrier cannot be dropped without blocking until all checkpoints are cleared. Generating more than n Checkpoints results in a panic. Generating less than n Checkpoints will result in an error being returned from wait(). If a Checkpoint is passed by a panicking thread, wait() will return an error.


use pool_barrier::{Barrier, ActiveBarrier};

const THREADS: usize = 5;

let mut barrier = Barrier::new(THREADS);
run(barrier.activate());                            // a barrier can be reused once checkpoints are cleared

fn run(mut barrier: ActiveBarrier){
    for i in 0..THREADS{
        let mut checkpoint = barrier.checkpoint();
            println!("thread_id: {}", i);           // all of these occur in arbitrary order
            checkpoint.check_in();                  // this does not block the spawned thread
    barrier.wait().unwrap();                        // main thread blocks here until all checkpoints are cleared
    println!("main thread");                        // this occurs last 



An ActiveBarrier can be used to generate checkpoints which must be cleared (usually by worker threads) before wait() and drop() unblock.


A stack allocated synchronisation barrier. See crate doc for use.


A checkpoint which must be cleared, by calling check_in(). All checkpoints must be cleared before wait() on the parent ActiveBarrier unblocks. Can be sent to other threads. Automatically calls check_in() when dropped.



Type Definitions