1 use rayon::prelude::*;
2 use rayon::ThreadPoolBuilder;
3 
4 #[test]
cross_pool_busy()5 fn cross_pool_busy() {
6     let pool1 = ThreadPoolBuilder::new().num_threads(1).build().unwrap();
7     let pool2 = ThreadPoolBuilder::new().num_threads(1).build().unwrap();
8 
9     let n: i32 = 100;
10     let sum: i32 = pool1.install(move || {
11         // Each item will block on pool2, but pool1 can continue processing other work from the
12         // parallel iterator in the meantime. There's a chance that pool1 will still be awake to
13         // see the latch set without being tickled, and then it will drop that stack job. The latch
14         // internals must not assume that the job will still be alive after it's set!
15         (1..=n)
16             .into_par_iter()
17             .map(|i| pool2.install(move || i))
18             .sum()
19     });
20     assert_eq!(sum, n * (n + 1) / 2);
21 }
22