ThreadPool

Struct ThreadPool 

Source
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

Source

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.

Source

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.

Source

pub fn num_threads(&self) -> usize

The total number of worker threads in this pool.

Source

pub fn join<A, B, RA, RB>(&self, oper_a: A, oper_b: B) -> (RA, RB)
where A: FnOnce() -> RA + Send, B: FnOnce() -> RB + Send, RA: Send, RB: Send,

Takes two closures and potentially runs them in parallel. It returns a pair of the results from those closures.

Source

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.

Source

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.

Source

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.

Source

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.

Trait Implementations§

Source§

impl Drop for ThreadPool

Source§

fn drop(&mut self)

Executes the destructor for this type. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.