rayon-rs / rayon

Rayon: A data parallelism library for Rust
Apache License 2.0
10.83k stars 493 forks source link

exit_handler is not run for the global thread pool #483

Open Yamakaky opened 6 years ago

Yamakaky commented 6 years ago

If I add in my main:

let rayon_conf = Configuration::new().start_handler(|i| println!("{}", i)).exit_handler(|i| println!("{}", i));
rayon::initialize(rayon_conf).unwrap();

the start_handler is run, but not the exit_handler. It works correctly if I use a ThreadPool.

cuviper commented 6 years ago

I think technically, the global thread pool is never stopped. Values in Rust static variables never run their destructors, and only relatively recently supported Drop types at all (RFC 1440). IIRC non-main threads will just terminate immediately when the process exits, so no, they won't run exit_handler.

In fact, I think even ThreadPool's Drop does not synchronously stop threads, just signals for them to "gradually terminate". I think we could add a synchronous terminate method though, if you really need to make sure the exit_handler runs.

Yamakaky commented 6 years ago

Yeah, that's what I thought. I need it to run https://docs.rs/flame/0.2.0/flame/fn.commit_thread.html. Flame records timing in a threadlocal struct and adds it to the global store to prevent locking too much. If this function isn't run, I only see the timings for the main lock.

cuviper commented 6 years ago

If you get a chance to try #492, I think that will work for your commit_thread.

I'm also playing with a way to sync the thread exits, to make sure a given pool is totally done, but I haven't figured out a way to safely shut down the global pool yet.