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 a6b333c
Showing 2 changed files with 15 additions and 0 deletions.
9 changes: 9 additions & 0 deletions rayon-core/src/lib.rs
Original file line number Diff line number Diff line change
@@ -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 {
6 changes: 6 additions & 0 deletions rayon-core/src/thread_pool/mod.rs
Original file line number Diff line number Diff line change
@@ -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 {

0 comments on commit a6b333c

Please sign in to comment.