Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Give futures::executor::ThreadPool a possibility to wait for completion #2461

Open
TobiasJacob opened this issue Jun 29, 2021 · 0 comments
Open
Labels
A-executor Area: futures::executor C-feature-request

Comments

@TobiasJacob
Copy link

Consider the following example for the threadpool, which is close to the example from the documentation:

https://docs.rs/futures/0.3.15/futures/executor/struct.ThreadPool.html

use std::thread::sleep;
use std::time::Duration;

use futures::FutureExt;
use futures::executor::ThreadPool;

async fn cpu_intensive(i: u64) {
    let mut res: u64 = 0;
    for j in 0..23 {
        for k in 0..256 {
            res += j + i + k;
            res %= 91349;
        }
        // Imagine that here an async API call is required, so we want another future to continue while this future is waiting for the API result 
    }
    println!("{}: {}", i, res)
}

fn main() {
    let pool = ThreadPool::new().unwrap();
    for i in 0..1000 {
        let fut = cpu_intensive(i);
        pool.spawn_ok(fut)
    }
    // NEEDED: A way to wait for the thread pool to run until completion
    sleep(Duration::from_secs(2));
}

It would be great if there was a way to block main until all futures of the ThreadPool are completed.

@taiki-e taiki-e added A-executor Area: futures::executor C-feature-request labels Jul 7, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-executor Area: futures::executor C-feature-request
Projects
None yet
Development

No branches or pull requests

2 participants