Struct ThreadPool

Source
pub struct ThreadPool<'env, M: ThreadManager<'env>, T: Send + 'static> { /* private fields */ }
Expand description

Core thread pool.

Implementations§

Source§

impl<'env, M: ThreadManager<'env>, T: Send> ThreadPool<'env, M, T>

Source

pub fn new(n_threads: usize) -> Self

Creates a new thread pool

§Arguments
  • n_threads: maximum number of threads allowed to run at the same time.

returns: ThreadPool<T, Manager>

§Examples
use bp3d_threads::UnscopedThreadManager;
use bp3d_threads::ThreadPool;
let _: ThreadPool<UnscopedThreadManager, ()> = ThreadPool::new(4);
Source

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, |_| ());
Source

pub fn dispatch<F: FnOnce(usize) -> T + Send + 'env>( &mut self, manager: &M, f: F, ) -> bool

👎Deprecated since 1.1.0: 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

Source

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.

Source

pub fn poll(&mut self) -> Option<T>

Poll a result from this thread pool if any, returns None if no result is available.

Source

pub fn reduce(&mut self) -> Iter<'_, 'env, M, 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.

Source

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.

Source

pub fn join(&mut self) -> Result<()>

👎Deprecated since 1.1.0: 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> Freeze for ThreadPool<'env, M, T>

§

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§

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> 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> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
Source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
Source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. 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.