[][src]Crate threadpool_scope

This crate provides an interface based on the scoped-threadpool crate adapted for use with the threadpool crate's 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:

use threadpool::ThreadPool;
use threadpool_scope::scope_with;

fn main() {
    // Create a threadpool holding 4 threads
    let pool = ThreadPool::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
    scope_with(&pool, |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

Scope

Functions

scope_with

Borrows a threadpool creating a Scope which can be used to execute non-'static closures which may borrow from the scope of scope_with.