Struct ThreadPool

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

A thread pool.

This can be cheaply cloned to create more handles to the same thread pool, so there is no need to wrap it in an Arc or similar type.

When dropped, the destructor won’t block but the thread pool itself will continue to run and process tasks. You can call ThreadPool::wait_all_complete to wait for all the active tasks to complete.

Implementations§

Source§

impl ThreadPool

Source

pub fn new() -> Self

Construct a new thread pool using the default configuration. See Builder for how to customize it further.

Source

pub fn builder() -> Builder

Create a Builder for customizing a thread pool.

Source

pub unsafe fn spawn_raw<T>(&self, data: *const T, run: unsafe fn(*const T))

Raw API for spawning a function on the thread pool.

This will call the given function pointer with the provided data pointer on the blocking thread pool at some point in the future.

§Safety

When called with the provided data pointer from another thread, the run function must not cause UB or panic.

Source

pub fn spawn_boxed<F: FnOnce() + Send + 'static>(&self, f: Box<F>)

Spawn a boxed closure on the thread pool.

§Examples
let pool = blocking_pool::ThreadPool::new();
pool.spawn_boxed(Box::new(|| println!("Hello world")));
Source

pub fn wait_all_complete(&self)

Wait for all the running and queued tasks in the thread pool to complete.

§Examples
use std::time::{Duration, Instant};

let pool = blocking_pool::ThreadPool::new();

pool.spawn_boxed(Box::new(|| std::thread::sleep(Duration::from_secs(3))));

let start = Instant::now();
pool.wait_all_complete();
assert!(start.elapsed().as_secs() >= 3);
Source

pub fn prune(&self)

Prune unused threads. These threads would time out and exit eventually of their own accord if they are not receiving any work, but this will force them to exit early.

§Examples
let pool = blocking_pool::ThreadPool::new();

// This will start up a thread that will linger around even after the task is finished.
pool.spawn_boxed(Box::new(|| {}));

// This will forcibly kill that thread.
pool.prune();

Trait Implementations§

Source§

impl Clone for ThreadPool

Source§

fn clone(&self) -> ThreadPool

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 Debug for ThreadPool

Source§

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

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

impl Default for ThreadPool

Source§

fn default() -> Self

Returns the “default value” for a type. 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> 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.