[][src]Crate scoped_threadpool

This crate provides a stable, safe and scoped threadpool.

It can be used to execute a number of short-lived jobs in parallel without the need to respawn the underlying threads.

Jobs are runnable by borrowing the pool for a given scope, during which an arbitrary number of them can be executed. These jobs can access data of any lifetime outside of the pools scope, which allows working on non-'static references in parallel.

For safety reasons, a panic inside a worker thread will not be isolated, but rather propagate to the outside of the pool.

Examples:

extern crate scoped_threadpool;
use scoped_threadpool::Pool;

fn main() {
    // Create a threadpool holding 4 threads
    let mut pool = Pool::new(4);

    let mut vec = vec![0, 1, 2, 3, 4, 5, 6, 7];

    // Use the threads as scoped threads that can
    // reference anything outside this closure
    pool.scoped(|scope| {
        // Create references to each element in the vector ...
        for e in &mut vec {
            // ... and add 1 to it in a seperate thread
            scope.execute(move || {
                *e += 1;
            });
        }
    });

    assert_eq!(vec, vec![1, 2, 3, 4, 5, 6, 7, 8]);
}

Structs

Pool

A threadpool that acts as a handle to a number of threads spawned at construction.

Scope

Handle to the scope during which the threadpool is borrowed.