Skip to content

Commit 95b44e4

Browse files
committed
Add a mean to get the current threadpool
It is useful when you want to share the same threadpool across threads outside the threadpool.
1 parent 385840e commit 95b44e4

File tree

3 files changed

+16
-1
lines changed

3 files changed

+16
-1
lines changed

rayon-core/src/lib.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,15 @@ pub fn current_num_threads() -> usize {
109109
crate::registry::Registry::current_num_threads()
110110
}
111111

112+
/// Returns the current threadpool.
113+
///
114+
/// It is a shorthand for [`Threadpool::current()`][tc].
115+
///
116+
/// [tc]: struct.ThreadPool.html#method.current
117+
pub fn current() -> ThreadPool {
118+
ThreadPool::current()
119+
}
120+
112121
/// Error when initializing a thread pool.
113122
#[derive(Debug)]
114123
pub struct ThreadPoolBuildError {

rayon-core/src/thread_pool/mod.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -252,6 +252,12 @@ impl ThreadPool {
252252
// We assert that `self.registry` has not terminated.
253253
unsafe { spawn::spawn_fifo_in(op, &self.registry) }
254254
}
255+
256+
/// Returns the current threadpool.
257+
pub fn current() -> ThreadPool {
258+
let registry = crate::registry::Registry::current();
259+
ThreadPool { registry }
260+
}
255261
}
256262

257263
impl Drop for ThreadPool {

src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ pub use rayon_core::ThreadBuilder;
114114
pub use rayon_core::ThreadPool;
115115
pub use rayon_core::ThreadPoolBuildError;
116116
pub use rayon_core::ThreadPoolBuilder;
117-
pub use rayon_core::{current_num_threads, current_thread_index};
117+
pub use rayon_core::{current, current_num_threads, current_thread_index};
118118
pub use rayon_core::{join, join_context};
119119
pub use rayon_core::{scope, Scope};
120120
pub use rayon_core::{scope_fifo, ScopeFifo};

0 commit comments

Comments
 (0)