pub struct ThreadPool { /* private fields */ }Expand description
Thread pool that allows to change number of threads at runtime.
On Drop it instructs threads to shutdown, but doesn’t await for them to finish
§Note
The pool doesn’t implement any sort of flow control. If workers are busy, message will remain in queue until any other thread can take it.
§Clone
Thread pool intentionally doesn’t implement Clone
If you want to share it, then share it by using global variable or on heap.
It is thread safe, so concurrent access is allowed.
§Panic
Each thread wraps execution of job into catch_unwind to ensure that thread is not aborted
on panic
Implementations§
Source§impl ThreadPool
impl ThreadPool
Sourcepub const fn with_defaults(name: &'static str, stack_size: usize) -> Self
pub const fn with_defaults(name: &'static str, stack_size: usize) -> Self
Creates new instance by specifying all params
Sourcepub fn set_stack_size(&self, stack_size: usize) -> usize
pub fn set_stack_size(&self, stack_size: usize) -> usize
Sets stack size to use.
By default it uses default value, used by Rust’s stdlib. But setting this variable overrides it, allowing to customize it.
This setting takes effect only when creating new threads
Sourcepub fn set_threads(&self, thread_num: u16) -> Result<u16>
pub fn set_threads(&self, thread_num: u16) -> Result<u16>
Sets worker number, starting new threads if it is greater than previous
In case if it is less, extra threads are shut down. Returns previous number of threads.
By default when pool is created no threads are started.
If any thread fails to start, function returns immediately with error.
§Note
Any calls to this method are serialized, which means under hood it locks out any attempt to change number of threads, until it is done
Sourcepub fn shutdown(&mut self)
pub fn shutdown(&mut self)
Terminates all threads and clears internal state
Mutable access guarantees that only one writer can clear state without need of internal lock
Sourcepub fn shutdown_and_join(&mut self)
pub fn shutdown_and_join(&mut self)
Terminates all threads, awaiting their completion and clears internal state
Mutable access guarantees that only one writer can clear state without need of internal lock