Crate futures_cpupool [] [src]

A simple crate for executing work on a thread pool, and getting back a future.

This crate provides a simple thread pool abstraction for running work externally from the current thread that's running. An instance of Future is handed back to represent that the work may be done later, and futher computations can be chained along with it as well.

extern crate futures;
extern crate futures_cpupool;

use std::sync::mpsc::channel;

use futures::{Future, Task};
use futures_cpupool::CpuPool;


// Create a worker thread pool with four threads
let pool = CpuPool::new(4);

// Execute some work on the thread pool, optionally closing over data.
let a = pool.execute(long_running_computation);
let b = pool.execute(|| long_running_computation2(100));

// Express some further computation once the work is completed on the thread
// pool.
let c = a.join(b).map(|(a, b)| a + b);

// Block the current thread to get the result.
let (tx, rx) = channel();
Task::new().run(c.then(move |res| {
    tx.send(res).unwrap();
    Ok(())
}).boxed());
let res = rx.recv().unwrap();

// Print out the result
println!("{:?}", res);

Structs

CpuFuture

The type of future returned from the CpuPool::execute function.

CpuPool

A thread pool intended to run CPU intensive work.