Struct stateful_async_worker::ThreadPool[][src]

pub struct ThreadPool<ThreadState, Result> { /* fields omitted */ }

Abstracts a pool of stateful background worker threads, that can run synchronous functions and provide the return value as asynchronous future.

The thread pool owns an arbitrary ThreadState, which will be passed as a reference to the called function. Thus the asynchronous code can easily access and mutate state when necessary.

Unlike a single worker thread, the thread pool can process multiple function at a time. This, however, forces the closures to take a non-mutable reference to the shared state. For mutable access interior mutability, e.g. via RwLock, has to be used.

Implementations

impl<ThreadState, Result> ThreadPool<ThreadState, Result> where
    ThreadState: Default + Send + Sync + 'static,
    Result: Send + 'static, 
[src]

pub fn spawn() -> Self[src]

Spawns a new thread for every logical CPU core. The state will be initialized with Default::default().

pub fn spawn_exactly(thread_count: usize) -> Self[src]

Spawns exactly thread_count threads. The state will be initialized with Default::default().

impl<ThreadState, Result> ThreadPool<ThreadState, Result> where
    ThreadState: Sync + Send + 'static,
    Result: Send + 'static, 
[src]

pub fn spawn_with<T>(data: T) -> Self where
    T: Into<Arc<ThreadState>>, 
[src]

Spawns a new thread for every logical CPU core. The state will be initialized with data.

pub fn spawn_exactly_with<T>(data: T, thread_count: usize) -> Self where
    T: Into<Arc<ThreadState>>, 
[src]

Spawns exactly thread_count threads. The state will be initialized with data.

pub async fn work_on<F>(&self, func: F) -> Result where
    F: FnOnce(&ThreadState) -> Result + Send + 'static, 
[src]

Pass a synchronous function, so the thread pool can execute it. Execution will be begin even before the first call to poll.

pub async fn work_on_boxed(
    &self,
    func: Box<dyn FnOnce(&ThreadState) -> Result + Send + 'static>
) -> Result
[src]

Like work_on but for functions that are already boxed.

Trait Implementations

impl<ThreadState, Result> Clone for ThreadPool<ThreadState, Result>[src]

Auto Trait Implementations

impl<ThreadState, Result> RefUnwindSafe for ThreadPool<ThreadState, Result>[src]

impl<ThreadState, Result> Send for ThreadPool<ThreadState, Result> where
    Result: Send
[src]

impl<ThreadState, Result> Sync for ThreadPool<ThreadState, Result> where
    Result: Send
[src]

impl<ThreadState, Result> Unpin for ThreadPool<ThreadState, Result>[src]

impl<ThreadState, Result> UnwindSafe for ThreadPool<ThreadState, Result>[src]

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.