Where does ParIterator run in Rayon Threadpool spawn

use rayon::prelude::*; // 1.5.3
fn main() {
    let pool = rayon::ThreadPoolBuilder::new().num_threads(4).build().unwrap();
    pool.spawn(|| {
        let n: u32 = (0..11).into_par_iter().map(|x| x + 1).sum();
        println!("{}", n);


Do the into_par_iter tasks created in the spawn closure run in the created thread pool or the global pool?

No, to do that you have to use the install method.

Parallel iterators will run in the "current" thread pool. If you're already running in a pool (like your pool.spawn) then the parallel iterator will use that too, otherwise it uses the global pool.

But in your example, main will not wait for the spawn to complete, so this probably isn't what you want. Then a blocking install might be a better choice, or maybe in_place_scope to spawn and then do other things in the main thread before the scope returns.

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.