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
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 0 deletions.
9 changes: 9 additions & 0 deletions rayon-core/src/lib.rs
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
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

0 comments on commit a6b333c

Please sign in to comment.