Struct stateful_async_worker::ThreadPool [−][src]
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]
ThreadState: Default + Send + Sync + 'static,
Result: Send + 'static,
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]
ThreadState: Sync + Send + 'static,
Result: Send + 'static,
pub fn spawn_with<T>(data: T) -> Self where
T: Into<Arc<ThreadState>>,
[src]
T: Into<Arc<ThreadState>>,
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]
T: Into<Arc<ThreadState>>,
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]
F: FnOnce(&ThreadState) -> Result + Send + 'static,
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]
&self,
func: Box<dyn FnOnce(&ThreadState) -> Result + Send + 'static>
) -> Result
Like work_on
but for functions that are already boxed.
Trait Implementations
impl<ThreadState, Result> Clone for ThreadPool<ThreadState, Result>
[src]
fn clone(&self) -> Self
[src]
pub fn clone_from(&mut self, source: &Self)
1.0.0[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]
Result: Send,
impl<ThreadState, Result> Sync for ThreadPool<ThreadState, Result> where
Result: Send,
[src]
Result: Send,
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]
T: 'static + ?Sized,
impl<T> Borrow<T> for T where
T: ?Sized,
[src]
T: ?Sized,
impl<T> BorrowMut<T> for T where
T: ?Sized,
[src]
T: ?Sized,
pub fn borrow_mut(&mut self) -> &mut T
[src]
impl<T> From<T> for T
[src]
impl<T, U> Into<U> for T where
U: From<T>,
[src]
U: From<T>,
impl<T> ToOwned for T where
T: Clone,
[src]
T: Clone,
type Owned = T
The resulting type after obtaining ownership.
pub fn to_owned(&self) -> T
[src]
pub fn clone_into(&self, target: &mut T)
[src]
impl<T, U> TryFrom<U> for T where
U: Into<T>,
[src]
U: Into<T>,
type Error = Infallible
The type returned in the event of a conversion error.
pub fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>
[src]
impl<T, U> TryInto<U> for T where
U: TryFrom<T>,
[src]
U: TryFrom<T>,