pub struct ThreadPool { /* private fields */ }Expand description
A thread pool for executing blocking operations.
The number of worker threads scales dynamically with load, but will
never exceed the thread_limit parameter. When all worker threads are
busy, tasks enter an unbounded queue until a worker becomes available.
Clones share the workers, queue, and metrics.
Implementations§
Source§impl ThreadPool
impl ThreadPool
Sourcepub fn new(name: &str, thread_limit: usize, recv_timeout: Duration) -> Self
pub fn new(name: &str, thread_limit: usize, recv_timeout: Duration) -> Self
Creates a pool with a worker limit and idle-worker timeout.
Workers are started on demand and named using name. An idle worker
exits after recv_timeout without work. A thread_limit of zero is
treated as one.
Sourcepub fn metrics(&self) -> PoolMetrics
pub fn metrics(&self) -> PoolMetrics
Returns a snapshot of the pool metrics.
Gauges and totals are read independently and may be slightly inconsistent with each other.
Sourcepub fn execute<F, R>(&self, f: F) -> BlockingResult<R> ⓘ
pub fn execute<F, R>(&self, f: F) -> BlockingResult<R> ⓘ
Submits a closure to the thread pool.
The task will be executed by an available worker thread. If no threads are available and the pool has reached its maximum size, the work will be queued until a worker thread becomes available. Submission does not wait for the closure to run or for a worker slot, though it may start an OS thread.
Dropping the result cancels work not yet picked up by a worker; use
BlockingResult::detach to keep that work queued. Closure panics and
failures to start a worker when none is available yield BlockingError.