Struct std::sync::TaskPoolUnstable [-] [+] [src]

pub struct TaskPool {
    // 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

fn main() { use std::sync::TaskPool; use std::iter::AdditiveIterator; use std::sync::mpsc::channel; let pool = TaskPool::new(4); let (tx, rx) = channel(); for _ in 0..8 { let tx = tx.clone(); pool.execute(move|| { tx.send(1_u32).unwrap(); }); } assert_eq!(rx.iter().take(8).sum(), 8); }
use std::sync::TaskPool;
use std::iter::AdditiveIterator;
use std::sync::mpsc::channel;

let pool = TaskPool::new(4);

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

assert_eq!(rx.iter().take(8).sum(), 8);

Methods

impl TaskPool

fn new(threads: usize) -> TaskPool

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(), F: Send + 'static

Executes the function job on a thread in the pool.