pub struct ThreadPool { /* private fields */ }Expand description
Represents a user-created thread pool.
Use a ThreadPoolBuilder to specify the number and/or names of threads
in the pool. After calling ThreadPoolBuilder::build(), you can then
execute functions explicitly within this ThreadPool using
ThreadPool::install().
install() executes a closure in one of the ThreadPool’s
threads. In addition, any other scheduler operations called inside of install() will also
execute in the context of the ThreadPool.
When the ThreadPool is dropped, that’s a signal for the threads it manages to terminate,
they will complete executing any remaining work that you have spawned, and automatically
terminate.
Implementations§
Source§impl ThreadPool
impl ThreadPool
Sourcepub fn install<OP, R>(&self, op: OP) -> R
pub fn install<OP, R>(&self, op: OP) -> R
Executes op within the thread pool. Any attempts to use
join will then operate within that thread pool.
§Warning: thread-local data
Because op is executing within the thread pool,
thread-local data from the current thread will not be
accessible.
§Warning: execution order
If the current thread is part of a different thread pool, it will try to
keep busy while the op completes in its target pool, similar to
calling ThreadPool::yield_now() in a loop. Therefore, it may
potentially schedule other tasks to run on the current thread in the
meantime.
§Panics
If op should panic, that panic will be propagated.
Sourcepub fn broadcast<OP, R>(&self, op: OP) -> Vec<R>
pub fn broadcast<OP, R>(&self, op: OP) -> Vec<R>
Executes op within every thread in the thread pool. Any attempts to use
join will then operate within that thread pool.
Broadcasts are executed on each thread after they have exhausted their local work queue, before they attempt work-stealing from other threads. The goal of that strategy is to run everywhere in a timely manner without being too disruptive to current work.
§Warning: thread-local data
Because op is executing within the thread pool,
thread-local data from the current thread will not be
accessible.
§Panics
If op should panic on one or more threads, exactly one panic
will be propagated, only after all threads have completed
(or panicked) their own op.
Sourcepub fn current_num_threads(&self) -> usize
pub fn current_num_threads(&self) -> usize
Returns the number of threads in the thread pool.
Sourcepub fn current_thread_index(&self) -> Option<usize>
pub fn current_thread_index(&self) -> Option<usize>
If called from a worker thread in this thread pool,
returns the index of that thread; if not called from a worker
thread, or called from a worker that belongs to a
different thread pool, returns None.
The index for a given thread will not change over the thread’s lifetime. However, multiple threads may share the same index if they are in distinct thread pools.
Sourcepub fn spawn<OP>(&self, op: OP)
pub fn spawn<OP>(&self, op: OP)
Spawns an asynchronous task in this thread pool. This task may
outlast the current stack frame, so it cannot capture any references
onto the stack (you will likely need a move closure).
§Panics
If the task panics, the process aborts, since the panic has nowhere to propagate.
Sourcepub fn spawn_broadcast<OP>(&self, op: OP)
pub fn spawn_broadcast<OP>(&self, op: OP)
Spawns an asynchronous task on every thread in this thread pool. This task
may outlast the current stack frame, so it cannot capture any references
onto the stack (you will likely need a move closure).
§Panics
If the task panics on any thread, the process aborts, since the panic has nowhere to propagate.
Sourcepub fn yield_now(&self) -> Option<Yield>
pub fn yield_now(&self) -> Option<Yield>
Cooperatively yields execution to the scheduler.
Looks for and executes one unit of work if the current thread is part of this pool.
Returns Some(Yield::Executed) if anything was executed, Some(Yield::Idle) if
nothing was available, or None if the current thread is not part of this pool.