ThreadPool

Struct ThreadPool 

Source
pub struct ThreadPool<T> { /* private fields */ }
Expand description

Execute tasks on one of possibly several pooled threads.

For more details, see the library level documentation.

Implementations§

Source§

impl<T: Task> ThreadPool<T>

Source

pub fn fixed_size(size: usize) -> (Sender<T>, ThreadPool<T>)

Create a thread pool that reuses a fixed number of threads operating off a shared unbounded queue.

At any point, at most size threads will be active processing tasks. If additional tasks are submitted when all threads are active, they will wait in the queue until a thread is available. If any thread terminates due to a failure during execution prior to the thread pool shutting down, a new one will take its place if needed to execute subsequent tasks. The threads in the pool will exist until the thread pool is explicitly shutdown.

Source

pub fn single_thread() -> (Sender<T>, ThreadPool<T>)

Create a thread pool with a single worker thread operating off an unbounded queue.

Note, however, that if this single thread termintaes due to a failure during execution prior to the thread pool shutting down, a new one will take its place if needed to execute subsequent tasks. Tasks are guaranteed to execute sequentially, and no more than one task will be active at any given time.

Source

pub fn prestart_core_thread(&self) -> bool

Start a core thread, causing it to idly wait for work.

This overrides the default policy of starting core threads only when new tasks are executed. This function will return false if all core threads have already been started.

Source

pub fn prestart_core_threads(&self)

Start all core threads, causing them to idly wait for work.

This overrides the default policy of starting core threads only when new tasks are executed.

Source

pub fn shutdown(&self)

Initiate an orderly shutdown.

Any previously submitted tasks are executed, but no new tasks will be accepted. Invocation has no additional effect if the thread pool has already been shut down.

This function will not wait for previously submitted tasks to complete execution. Use await_termination to do that.

Source

pub fn shutdown_now(&self)

Shutdown the thread pool as fast as possible.

Worker threads will no longer receive tasks off of the work queue. This function will drain any remaining tasks before returning.

There are no guarantees beyond best-effort attempts to actually shutdown in a timely fashion. Threads will finish processing tasks that are currently running.

Source

pub fn is_terminating(&self) -> bool

Returns true if the thread pool is in the process of terminating but has not yet terminated.

Source

pub fn is_terminated(&self) -> bool

Returns true if the thread pool is currently terminated.

Source

pub fn await_termination(&self)

Blocks the current thread until the thread pool has terminated

Source

pub fn size(&self) -> usize

Returns the current number of running threads

Source

pub fn queued(&self) -> usize

Returns the current number of pending tasks

Trait Implementations§

Source§

impl<T> Clone for ThreadPool<T>

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<T> Debug for ThreadPool<T>

Source§

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

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl<T> Freeze for ThreadPool<T>

§

impl<T> !RefUnwindSafe for ThreadPool<T>

§

impl<T> Send for ThreadPool<T>
where T: Send,

§

impl<T> Sync for ThreadPool<T>
where T: Send,

§

impl<T> Unpin for ThreadPool<T>

§

impl<T> !UnwindSafe for ThreadPool<T>

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> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. 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> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

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

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

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.