Struct blocking_pool::ThreadPool[][src]

pub struct ThreadPool { /* fields omitted */ }

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

impl ThreadPool[src]

#[must_use]
pub fn new() -> Self
[src]

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

#[must_use]
pub fn builder() -> Builder
[src]

Create a Builder for customizing a thread pool.

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

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.

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

Spawn a boxed closure on the thread pool.

Examples

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

pub fn wait_all_complete(&self)[src]

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);

pub fn prune(&self)[src]

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

impl Clone for ThreadPool[src]

impl Debug for ThreadPool[src]

impl Default for ThreadPool[src]

Auto Trait Implementations

Blanket Implementations

impl<T> Any for T where
    T: 'static + ?Sized
[src]

impl<T> Borrow<T> for T where
    T: ?Sized
[src]

impl<T> BorrowMut<T> for T where
    T: ?Sized
[src]

impl<T> From<T> for T[src]

impl<T, U> Into<U> for T where
    U: From<T>, 
[src]

impl<T> ToOwned for T where
    T: Clone
[src]

type Owned = T

The resulting type after obtaining ownership.

impl<T, U> TryFrom<U> for T where
    U: Into<T>, 
[src]

type Error = Infallible

The type returned in the event of a conversion error.

impl<T, U> TryInto<U> for T where
    U: TryFrom<T>, 
[src]

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.