futures-cpupool 0.1.0

An implementation of thread pools which hand out futures to the results of the computation on the threads themselves.

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;

# fn long_running_computation() -> u32 { 2 }
# fn long_running_computation2(a: u32) -> u32 { a }
# fn main() {

// 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);
# }