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
impl ThreadPool
Sourcepub fn in_all_threads_mut(&self, f: Arc<Mutex<dyn FnMut() + Send + Sync>>)
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.
Sourcepub fn in_all_threads(&self, f: Arc<dyn Fn() + Send + Sync>)
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.
Sourcepub fn set_thread_local<T>(
&self,
local_key: &'static LocalKey<RefCell<T>>,
val: T,
)
pub fn set_thread_local<T>( &self, local_key: &'static LocalKey<RefCell<T>>, val: T, )
Initializes the local_key
to contain val
.
See ./examples/thread_local.rs
Sourcepub fn shutdown(&mut self)
pub fn shutdown(&mut self)
Shut down the thread pool.
This will shut down the thread pool according to configuration. When configured with
- ShutdownMode::Immediate - terminate each tread after completing the current task
- ShutdownMode::CompletePending - terminate after completing all pending tasks