rayon-rs/rayon

Why does ThreadPool block until a operation is finished?

codecnotsupported opened this issue · 1 comments

It would appear as though ThreadPool requires operations to be executed in order of submission, blocking other threads from execution until the last is finished.

fn main() {
    let thread_pool = ThreadPoolBuilder::new().num_threads(2).build().unwrap();

    thread_pool.install(test1);
    thread_pool.install(test2);
    thread_pool.install(test3);
    thread_pool.install(test4);
    thread_pool.install(test5);
    thread_pool.install(test6);

    std::thread::sleep(Duration::from_millis(200));
}
fn test1() {println!("1");}
fn test2() {thread::sleep(Duration::from_millis(100));println!("2");}
fn test3() {println!("3");}
fn test4() {println!("4");}
fn test5() {println!("5");}
fn test6() {println!("6");}

Will print

1
2
3
4
5
6

Instead of

1
3
4
5
6
2

Solution: ThreadPool::Spawn instead. And RTFM.