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

Core thread pool.

Implementations

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

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

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.

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

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.

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.

👎 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

Blanket Implementations

Gets the TypeId of self. Read more

Immutably borrows from an owned value. Read more

Mutably borrows from an owned value. Read more

Returns the argument unchanged.

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

The alignment of pointer.

The type for initializers.

Initializes a with the given initializer. Read more

Dereferences the given pointer. Read more

Mutably dereferences the given pointer. Read more

Drops the object pointed to by the given pointer. Read more

The type returned in the event of a conversion error.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.