pub struct ThreadPool { /* private fields */ }
Expand description

Execute tasks concurrently while maintaining bounds on memory consumption

To demonstrate the use case this implementation solves let’s consider a program that reads lines from a file and writes those lines to another file after some processing. The processing itself is stateless and can be done in parallel on each line, but the reading and writing must be sequential. Using this implementation we will read the input in the main thread, submit it for concurrent processing to a processing thread pool and collect it for writing in a writing thread pool with a single thread. See ./examples/read_process_write_pipeline.rs. The submission to a thread pool is done through a blocking bounded queue, so if the processing thread pool or the writing thread pool cannot keep up, their blocking queues will fill up and create a backpressure that will pause the reading. So the resulting pipeline will stabilize on a throughput commanded by the slowest stage with the memory consumption determined by sizes of queues and number of threads in each thread pool.

For reference see Command Pattern and Producer-Consumer

Implementations§

source§

impl ThreadPool

source

pub fn tasks(&self) -> usize

Set the number of concurrent threads in the thread pool

source

pub fn in_all_threads_mut(&self, f: Arc<Mutex<dyn FnMut() + Send + Sync>>)

Execute f in all threads.

This function returns only after f had completed in all threads. Can be used to collect data produced by the threads. See ./examples/fetch_thread_local.rs.

Caveat: this is a barrier function. So if one of the threads is busy with a long running task or is deadlocked, this will halt all the threads until f can be executed.

source

pub fn in_all_threads(&self, f: Arc<dyn Fn() + Send + Sync>)

Execute f in all threads.

This function returns only after f had completed in all threads. Can be used to flush data produced by the threads or simply execute work concurrently. See ./examples/flush_thread_local.rs.

Caveat: this is a barrier function. So if one of the threads is busy with a long running task or is deadlocked, this will halt all the threads until f can be executed.

source

pub fn set_thread_local<T>( &mut self, local_key: &'static LocalKey<RefCell<T>>, val: T )where T: Sync + Send + Clone,

Initializes the local_key to contain val.

See ./examples/thread_local.rs

source

pub fn shutdown(&mut self)

Shut down the thread pool.

This will shut down the thread pool according to configuration. When configured with

source

pub fn join(&mut self) -> Result<(), Error>

Wait until all thread pool threads completed.

source

pub fn submit(&self, command: Box<dyn Command + Send + Sync>)

Submit command for execution

source

pub fn try_submit( &self, command: Box<dyn Command + Send + Sync>, timeout: Duration ) -> Option<Box<dyn Command + Send + Sync>>

Submit command for execution with timeout

Returns the command on failure and None on success

Auto Trait Implementations§

Blanket Implementations§

source§

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

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

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

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for Twhere 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 Twhere 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 Twhere U: Into<T>,

§

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 Twhere U: TryFrom<T>,

§

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.