ThreadPool

Struct ThreadPool 

Source
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

Source

pub const fn new() -> Self

Creates new thread pool with default params

Source

pub const fn with_defaults(name: &'static str, stack_size: usize) -> Self

Creates new instance by specifying all params

Source

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

Source

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

Source

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

Source

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

Source

pub fn spawn<F: FnOnce() + Send + 'static>(&self, job: F)

Schedules new execution, sending it over to one of the workers.

Source

pub fn spawn_handle<R: Send + 'static, F: FnOnce() -> R + Send + 'static>( &self, job: F, ) -> JobHandle<R>

Schedules execution, that allows to await and receive it’s result.

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§

impl Sync for ThreadPool

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, 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.