1 | use rayon::prelude::*; |
2 | use std::iter::once_with; |
3 | |
4 | const N: usize = 100_000; |
5 | |
6 | #[test] |
7 | #[cfg_attr (any(target_os = "emscripten" , target_family = "wasm" ), ignore)] |
8 | fn par_bridge_recursion() { |
9 | let pool = rayon::ThreadPoolBuilder::new() |
10 | .num_threads(10) |
11 | .build() |
12 | .unwrap(); |
13 | |
14 | let seq: Vec<_> = (0..N).map(|i| (i, i.to_string())).collect(); |
15 | |
16 | pool.broadcast(|_| { |
17 | let mut par: Vec<_> = (0..N) |
18 | .into_par_iter() |
19 | .flat_map(|i| { |
20 | once_with(move || { |
21 | // Using rayon within the serial iterator creates an opportunity for |
22 | // work-stealing to make par_bridge's mutex accidentally recursive. |
23 | rayon::join(move || i, move || i.to_string()) |
24 | }) |
25 | .par_bridge() |
26 | }) |
27 | .collect(); |
28 | par.par_sort_unstable(); |
29 | assert_eq!(seq, par); |
30 | }); |
31 | } |
32 | |