pool_barrier 0.1.1

A barrier for blocking a main thread until the completion of work which has been offloaded to worker threads, without blocking the worker threads.
Documentation
  • Coverage
  • 76.47%
    13 out of 17 items documented1 out of 14 items with examples
  • Size
  • Source code size: 14.62 kB This is the summed size of all the files inside the crates.io package for this release.
  • Documentation size: 2.82 MB This is the summed size of all files generated by rustdoc for all configured targets
  • Ø build duration
  • this release: 9s Average build duration of successful builds.
  • all releases: 9s Average build duration of successful builds in releases after 2024-10-23.
  • Links
  • millardjn/pool_barrier
    4 0 0
  • crates.io
  • Dependencies
  • Versions
  • Owners
  • millardjn

pool_barrier

A barrier for blocking a main thread until the completion of work which has been offloaded to worker threads, without blocking the worker threads (in contrast to std::sync::Barrier which blocks all threads). Mainly useful for not deadlocking a threadpool by submitting more work items than there are threads.

docs

use pool_barrier::{Barrier, ActiveBarrier};

const THREADS: usize = 5;

let mut barrier = Barrier::new(THREADS);
run(barrier.activate());
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();
		std::thread::spawn(move||{
			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 
}