Struct threadpool::ThreadPool [] [src]

pub struct ThreadPool {
    // some fields omitted
}

A thread pool used to execute functions in parallel.

Spawns n worker threads and replenishes the pool if any worker threads panic.

Example

use threadpool::ThreadPool;
use std::sync::mpsc::channel;

let pool = ThreadPool::new(4);

let (tx, rx) = channel();
for i in 0..8 {
    let tx = tx.clone();
    pool.execute(move|| {
        tx.send(i).unwrap();
    });
}

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

Methods

impl ThreadPool
[src]

fn new(threads: usize) -> ThreadPool

Spawns a new thread pool with threads threads.

Panics

This function will panic if threads is 0.

fn execute<F>(&self, job: F) where F: FnOnce() + Send + 'static

Executes the function job on a thread in the pool.