Skip to main content

ThreadPool

Struct ThreadPool 

Source
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

Source

pub fn install<OP, R>(&self, op: OP) -> R
where OP: FnOnce() -> R + Send, R: Send,

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.

Source

pub fn broadcast<OP, R>(&self, op: OP) -> Vec<R>
where OP: Fn(BroadcastContext<'_>) -> R + Sync, R: Send,

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.

Source

pub fn current_num_threads(&self) -> usize

Returns the number of threads in the thread pool.

Source

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.

Source

pub fn spawn<OP>(&self, op: OP)
where OP: FnOnce() + Send + 'static,

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.

Source

pub fn spawn_broadcast<OP>(&self, op: OP)
where OP: Fn(BroadcastContext<'_>) + Send + Sync + 'static,

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.

Source

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.

Trait Implementations§

Source§

impl Debug for ThreadPool

Source§

fn fmt(&self, fmt: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Drop for ThreadPool

Source§

fn drop(&mut self)

Executes the destructor for this type. Read more
Source§

fn pin_drop(self: Pin<&mut Self>)

🔬This is a nightly-only experimental API. (pin_ergonomics)
Execute the destructor for this type, but different to Drop::drop, it requires self to be pinned. Read more

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 = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

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.