Module crossbeam_channel_pool

Module crossbeam_channel_pool 

Source
Expand description

A thread pool Executor used to execute functions in parallel.

Spawns a specified number of worker threads and replenishes the pool if any worker threads panic.

The pool automatically shuts down all workers when the last handle is dropped.

The interface is compatible with the standard threadpool, but the implementation runs faster, especially with multiple workers.

Uses crossbeam-channel internally for work distribution.

§Examples

§Synchronized with a channel

Every thread sends one message over the channel, which then is collected with the take().

use executors::*;
use executors::crossbeam_channel_pool::ThreadPool;
use std::sync::mpsc::channel;

let n_workers = 4;
let n_jobs = 8;
let pool = ThreadPool::new(n_workers);

let (tx, rx) = channel();
for _ in 0..n_jobs {
    let tx = tx.clone();
    pool.execute(move|| {
        tx.send(1).expect("channel will be there waiting for the pool");
    });
}

assert_eq!(rx.iter().take(n_jobs).fold(0, |a, b| a + b), 8);

Structs§

ThreadPool
A handle to a crossbeam_channel_pool