Spawning threads in loops


#1

I have a loop that does some time consuming tasks for each entry. How do I spawn a thread for each entry in the loop without waiting for the thread to complete its task? Something like:

for i in myiter.iter() {
   thread::spawn { complicated_stuff(i) }
}

seems like it would wait to complete complicated_stuff(i) before going to the next i.


#2

What about this…?

for i in myiter.iter() {
    thread::spawn(move || {
        complicated_stuff(i);
    });
}

#3

spawn() is non-blocking - it just launches the thread in the background, and returns you a JoinHandle that you can use to (a) wait for its completion and (b) get the result (or error) returned by the thread fn (if it has one). You can see in this playground example how the timing looks.


#4

You probably want to use rayon:

myiter.par_iter().for_each(complicated_stuff);
// done by now

#5

Many thanks to each of you. move seems to be the missing ingredient there.

@kornel: yes, rayon seems like a perfect fit. thanks. I edited the gist from @vitalyd to use rayon here. (I’m including it mostly as a bookmark for myself.)


#6

Use rayon threadpools. Spawning too many threads can be inefficient.