Explosion Pills
Explosion Pills

Reputation: 191729

Preferred method for awaiting concurrent threads

I have a program that loops over HTTP responses. These don't depend on each other, so they can be done simultaneously. I am using threads to do this:

extern crate hyper;

use std::thread;
use std::sync::Arc;
use hyper::Client;

fn main() {
    let client = Arc::new(Client::new());
    for num in 0..10 {
        let client_helper = client.clone();
        thread::spawn(move || {
            client_helper.get(&format!("http://example.com/{}", num))
             .send().unwrap();
        }).join().unwrap();
    }
}

This works, but I can see other possibilities of doing this such as:

let mut threads = vec![];

threads.push(thread::spawn(move || {
/* snip */
for thread in threads {
    let _ = thread.join();
}

It would also make sense to me to use a function that returns the thread handler, but I couldn't figure out how to do that ... not sure what the return type has to be.

What is the optimal/recommended way to wait for concurrent threads in Rust?

Upvotes: 1

Views: 84

Answers (1)

user395760
user395760

Reputation:

Your first program does not actually have any parallelism. Each time you spin up a worker thread, you immediately wait for it to finish before you start the next one. This is, of course, worse than useless.

The second way works, but there are crates that do some of the busywork for you. For example, scoped_threadpool and crossbeam have thread pools that allow you to write something like (untested, may contain mistakes):

let client = &Client::new();// No Arc needed
run_in_pool(|scope| {
    for num in 0..10 {
        scope.spawn(move || {
            client.get(&format!("http://example.com/{}", num)).send().unwrap();
        }
    }
})

Upvotes: 3

Related Questions