A thread pool used to execute functions in parallel.
Spawns a specified number of worker threads and replenishes the pool if any worker threads panic.
Examples
Synchronized with a channel
Every thread sends one message over the channel, which then is collected with the take()
.
use ThreadPool;
use channel;
let n_workers = 4;
let n_jobs = 8;
let pool = new;
let = channel;
for _ in 0..n_jobs
assert_eq!;
Synchronized with a barrier
Keep in mind, if a barrier synchronizes more jobs than you have workers in the pool, you will end up with a deadlock at the barrier which is not considered unsafe.
use ThreadPool;
use ;
use ;
// create at least as many workers as jobs or you will deadlock yourself
let n_workers = 42;
let n_jobs = 23;
let pool = new;
let an_atomic = new;
assert!;
// create a barrier that waits for all jobs plus the starter thread
let barrier = new;
for _ in 0..n_jobs
// wait for the threads to finish the work
barrier.wait;
assert_eq!;