pub struct ThreadPool { /* private fields */ }Expand description
Represents a user-created thread pool.
Use a ThreadPoolBuilder to specify the number and/or names of threads
in the pool. After calling ThreadPoolBuilder::build(), you can then
execute functions explicitly within this ThreadPool using
ThreadPool::install(). By contrast, top-level functions
(like join()) will execute implicitly within the current thread pool.
Implementations§
Source§impl ThreadPool
impl ThreadPool
Sourcepub fn install<R>(&self, f: impl FnOnce() -> R) -> R
pub fn install<R>(&self, f: impl FnOnce() -> R) -> R
Changes the current context to this thread pool. Any attempts to use
crate::join or parallel iterators will operate within this pool.
Panics if called from within a parallel iterator or other asynchronous task.
Sourcepub fn spawn<T: 'static + Send>(
&self,
f: impl 'static + FnOnce() -> T + Send,
) -> Task<T>
pub fn spawn<T: 'static + Send>( &self, f: impl 'static + FnOnce() -> T + Send, ) -> Task<T>
Spawns an asynchronous task on the global thread pool. The returned handle can be used to obtain the result.
Sourcepub fn num_threads(&self) -> usize
pub fn num_threads(&self) -> usize
The total number of worker threads in this pool.
Sourcepub fn join<A, B, RA, RB>(&self, oper_a: A, oper_b: B) -> (RA, RB)
pub fn join<A, B, RA, RB>(&self, oper_a: A, oper_b: B) -> (RA, RB)
Takes two closures and potentially runs them in parallel. It returns a pair of the results from those closures.
Sourcepub fn split_per_item(&self) -> impl '_ + GenericThreadPool
pub fn split_per_item(&self) -> impl '_ + GenericThreadPool
Execute paralight iterators with maximal parallelism.
Every iterator item may be processed on a separate thread.
Note: by maximizing parallelism, this also maximizes overhead.
This is best used with computationally-heavy iterators that have few
elements. For alternatives, see Self::split_per,
Self::split_by, and Self::split_by_threads.
Sourcepub fn split_per(&self, chunk_size: usize) -> impl '_ + GenericThreadPool
pub fn split_per(&self, chunk_size: usize) -> impl '_ + GenericThreadPool
Execute paralight iterators by batching elements.
Each group of chunk_size elements may be processed by a single thread.
Sourcepub fn split_by(&self, chunks: usize) -> impl '_ + GenericThreadPool
pub fn split_by(&self, chunks: usize) -> impl '_ + GenericThreadPool
Execute paralight iterators by batching elements.
Every iterator will be broken up into chunks
separate work units, which may be processed in parallel.
Sourcepub fn split_by_threads(&self) -> impl '_ + GenericThreadPool
pub fn split_by_threads(&self) -> impl '_ + GenericThreadPool
Execute paralight iterators by batching elements.
Every iterator will be broken up into Self::num_threads
separate work units, which may be processed in parallel.