Crates.io | pool_barrier |
lib.rs | pool_barrier |
version | 0.1.1 |
source | src |
created_at | 2017-06-02 23:31:00.57751 |
updated_at | 2017-06-02 23:43:18.398552 |
description | A barrier for blocking a main thread until the completion of work which has been offloaded to worker threads, without blocking the worker threads. |
homepage | |
repository | https://github.com/millardjn/pool_barrier |
max_upload_size | |
id | 17394 |
size | 12,200 |
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.
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
}