Struct ThreadPool

Source
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

Get 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>( &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 unchecked_submit(&self, command: Box<dyn Command + Send + Sync>)

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 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> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
Source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
Source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
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.