Skip to content

Commit a6b333c

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 a6b333c

File tree

2 files changed

+15
-0
lines changed

2 files changed

+15
-0
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 {

0 commit comments

Comments
 (0)