Struct bp3d_threads::ThreadPool
source · [−]pub struct ThreadPool<'env, M: ThreadManager<'env>, T: Send + 'static> { /* private fields */ }
Expand description
Core thread pool.
Implementations
sourceimpl<'env, M: ThreadManager<'env>, T: Send> ThreadPool<'env, M, T>
impl<'env, M: ThreadManager<'env>, T: Send> ThreadPool<'env, M, T>
sourcepub fn send<F: FnOnce(usize) -> T + Send + 'env>(&mut self, manager: &M, f: F)
pub fn send<F: FnOnce(usize) -> T + Send + 'env>(&mut self, manager: &M, f: F)
Send a new task to the injector queue.
The task execution order is not guaranteed, however the task index is guaranteed to be the order of the call to dispatch.
If a task panics it will leave a dead thread in the corresponding slot until .wait() is called.
Arguments
manager
: the thread manager to spawn a new thread if needed.f
: the task function to execute.
Examples
use bp3d_threads::UnscopedThreadManager;
use bp3d_threads::ThreadPool;
let manager = UnscopedThreadManager::new();
let mut pool: ThreadPool<UnscopedThreadManager, ()> = ThreadPool::new(4);
pool.send(&manager, |_| ());
sourcepub fn dispatch<F: FnOnce(usize) -> T + Send + 'env>(
&mut self,
manager: &M,
f: F
) -> bool
👎 Deprecated since 1.1.0: Please use send
instead
pub fn dispatch<F: FnOnce(usize) -> T + Send + 'env>(
&mut self,
manager: &M,
f: F
) -> bool
Please use send
instead
Schedule a new task to run.
Returns true if the task was successfully scheduled, false otherwise.
NOTE: Since version 1.1.0, failure is no longer possible so this function will never return false.
The task execution order is not guaranteed, however the task index is guaranteed to be the order of the call to dispatch.
If a task panics it will leave a dead thread in the corresponding slot until .join() is called.
Arguments
manager
: the thread manager to spawn a new thread if needed.f
: the task function to execute.
returns: bool
sourcepub fn is_idle(&self) -> bool
pub fn is_idle(&self) -> bool
Returns true if this thread pool is idle.
An idle thread pool does neither have running threads nor waiting tasks but may still have waiting results to poll.
sourcepub fn poll(&mut self) -> Option<T>
pub fn poll(&mut self) -> Option<T>
Poll a result from this thread pool if any, returns None if no result is available.
sourcepub fn reduce(&mut self) -> Iter<'_, 'env, M, T>ⓘNotable traits for Iter<'a, 'env, M, T>impl<'a, 'env, M: ThreadManager<'env>, T: Send + 'static> Iterator for Iter<'a, 'env, M, T> type Item = Result<T>;
pub fn reduce(&mut self) -> Iter<'_, 'env, M, T>ⓘNotable traits for Iter<'a, 'env, M, T>impl<'a, 'env, M: ThreadManager<'env>, T: Send + 'static> Iterator for Iter<'a, 'env, M, T> type Item = Result<T>;
Waits for all tasks to finish execution and stops all threads while iterating over task results.
Use this to periodically clean-up the thread pool, if you know that some tasks may panic.
Use this function in map-reduce kind of scenarios.
Errors
Returns an error if a thread did panic.
sourcepub fn wait(&mut self) -> Result<()>
pub fn wait(&mut self) -> Result<()>
Waits for all tasks to finish execution and stops all threads.
Use this to periodically clean-up the thread pool, if you know that some tasks may panic.
Errors
Returns an error if a thread did panic.
sourcepub fn join(&mut self) -> Result<()>
👎 Deprecated since 1.1.0: Please use wait
or reduce
instead
pub fn join(&mut self) -> Result<()>
Please use wait
or reduce
instead
Waits for all tasks to finish execution and stops all threads.
Use this to periodically clean-up the thread pool, if you know that some tasks may panic.
Errors
Returns an error if a thread did panic.
Auto Trait Implementations
impl<'env, M, T> !RefUnwindSafe for ThreadPool<'env, M, T>
impl<'env, M, T> Send for ThreadPool<'env, M, T> where
<M as ThreadManager<'env>>::Handle: Send,
impl<'env, M, T> Sync for ThreadPool<'env, M, T> where
T: Sync,
<M as ThreadManager<'env>>::Handle: Sync,
impl<'env, M, T> Unpin for ThreadPool<'env, M, T> where
T: Unpin,
impl<'env, M, T> !UnwindSafe for ThreadPool<'env, M, T>
Blanket Implementations
sourceimpl<T> BorrowMut<T> for T where
T: ?Sized,
impl<T> BorrowMut<T> for T where
T: ?Sized,
const: unstable · sourcefn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more