Spawning threads in loops


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.


What about this…?

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


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.


You probably want to use rayon:

// done by now


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.)


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