Skip to content

Commit

Permalink
Add a mean to get the current threadpool
Browse files Browse the repository at this point in the history
It is useful when you want to share the same threadpool across
threads outside the threadpool.
  • Loading branch information
lu-zero committed Mar 29, 2021
1 parent 385840e commit 95b44e4
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 1 deletion.
9 changes: 9 additions & 0 deletions rayon-core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,15 @@ pub fn current_num_threads() -> usize {
crate::registry::Registry::current_num_threads()
}

/// Returns the current threadpool.
///
/// It is a shorthand for [`Threadpool::current()`][tc].
///
/// [tc]: struct.ThreadPool.html#method.current
pub fn current() -> ThreadPool {
ThreadPool::current()
}

/// Error when initializing a thread pool.
#[derive(Debug)]
pub struct ThreadPoolBuildError {
Expand Down
6 changes: 6 additions & 0 deletions rayon-core/src/thread_pool/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,12 @@ impl ThreadPool {
// We assert that `self.registry` has not terminated.
unsafe { spawn::spawn_fifo_in(op, &self.registry) }
}

/// Returns the current threadpool.
pub fn current() -> ThreadPool {
let registry = crate::registry::Registry::current();
ThreadPool { registry }
}
}

impl Drop for ThreadPool {
Expand Down
2 changes: 1 addition & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ pub use rayon_core::ThreadBuilder;
pub use rayon_core::ThreadPool;
pub use rayon_core::ThreadPoolBuildError;
pub use rayon_core::ThreadPoolBuilder;
pub use rayon_core::{current_num_threads, current_thread_index};
pub use rayon_core::{current, current_num_threads, current_thread_index};
pub use rayon_core::{join, join_context};
pub use rayon_core::{scope, Scope};
pub use rayon_core::{scope_fifo, ScopeFifo};
Expand Down

0 comments on commit 95b44e4

Please sign in to comment.