rayon-rs / rayon

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

Add full_blocking feature to thread pool #1175

Open nhukc opened 3 months ago

nhukc commented 3 months ago

This PR adds an option full_blocking to ThreadPoolBuilder that affects the behavior of nested thread pools.

When a job on a parent thread pool creates a job in a child thread pool and full_blocking is true, the parent job will block until the child job is completed. This is different from the default behavior where the parent thread is allowed to work on other jobs in the parent thread pool while the child job completes.

This behavior is useful for avoiding deadlocks caused by work-stealing and is helpful for instrumentation based profiling in multi-threaded settings.

See for more details: https://github.com/rayon-rs/rayon/issues/1174

nhukc commented 3 months ago

Feel free to push directly to this PR.

The documentation could be improved by matching the language to the rest of the rayon docs.

I'm not 100% confident in the implementation of in_worker_cross_blocking. It works for my test, but I could easily be missing something in the details.

nhukc commented 3 months ago

Thanks for taking a look at the PR!

The most similar function in the ThreadPoolBuilder is

pub fn use_current_thread(self) -> ThreadPoolBuilder<S>

Which sets a bool to true when called. I matched that function signature.

JoeyBF commented 3 months ago

That's fair. I would argue that use_current_thread would also need a bool, but as far as this PR is concerned it's probably better to just be consistent.

nhukc commented 3 months ago

@cuviper Are you interested in merging this? If not, can you close it?

cuviper commented 3 months ago

I need some time to think about the implications of this change, to make sure there aren't any big foot-guns here. But you're also helping solve a foot-gun, so that's going to be a balancing act.

I might also bikeshed the name, but that's not a big deal.