Threadpool with thread results

Trying to find a threadpool that allows to retrieve the results of thread executions.

  1. Had a look at threadpool - Rust.

However, there seems to be not way to retrieve the execution result of a thread: threadpool::ThreadPool - Rust.

  1. Had a look at tokio-threadpool: tokio_threadpool - Rust

You can retrieve the result, but the basic example does not compile:

use futures::future::{Future, lazy};

fn main() {
  // Create a thread pool with default configuration values
  let thread_pool = tokio_threadpool::ThreadPool::new();

  thread_pool.spawn(lazy(|| {
      println!("called from a worker thread");

  // Gracefully shutdown the threadpool


error[E0593]: closure is expected to take 1 argument, but it takes 0 arguments
 --> src/
7 |   thread_pool.spawn(lazy(|| {
  |                     ^^^^ -- takes 0 arguments
  |                     |
  |                     expected closure that takes 1 argument

So assuming we fix this (doing lazy(|_|)) we still get:

error[E0277]: the trait bound `futures::future::Lazy<[closure@src/ 10:4]>: futures::future::Future` is not satisfied
  --> src/
7  |     thread_pool.spawn(lazy(|_| {
   |  _____________________^
8  | |       println!("called from a worker thread");
9  | |       Ok(())
10 | |   }));
   | |____^ the trait `futures::future::Future` is not implemented for `futures::future::Lazy<[closure@src/ 10:4]>`

error[E0599]: no method named `wait` found for struct `tokio_threadpool::Shutdown` in the current scope
   --> src/
13  |   thread_pool.shutdown().wait().unwrap();
    |                          ^^^^ method not found in `tokio_threadpool::Shutdown`

Seems to me this repo is unmaintained...

  1. The last solution I see is future-threadpool, but here again you don't get the return value: futures::executor::ThreadPool - Rust

Is there anything I am missing? Any solution for a working threadpool with return value out there?

The tokio-threadpool crate is extremely old. Use the modern tokio crate.

async fn main() {
    let task = tokio::spawn(async {
        println!("in task");
        10 + 5
    println!("Got value: {}", task.await.unwrap());

Depending on your application, it may also make sense to use the rayon crate.

1 Like

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.