ThreadPool

Struct ThreadPool 

Source
pub struct ThreadPool<T: Send + 'static> {
    sender: Option<SyncSender<Box<dyn FnOnce(usize) -> T + Send + 'static>>>,
    receiver: Arc<Mutex<Receiver<Box<dyn FnOnce(usize) -> T + Send + 'static>>>>,
    workers: Vec<JoinHandle<()>>,
    pub results: Arc<Mutex<Vec<T>>>,
    pending: Arc<AtomicUsize>,
    pending_pair: Arc<(Mutex<()>, Condvar)>,
    size: usize,
}
Expand description

A simple generic thread pool.

  • T: the return type of tasks. Completed task results are stored in results: Arc<Mutex<Vec<T>>>.
  • execute accepts a task and a block_if_full flag:
    • if true, submission will block when the pool is saturated until a worker becomes available;
    • if false, submission will return an error when the pool is saturated.
  • join waits until all submitted tasks have completed (it does not shut down the pool).

Fields§

§sender: Option<SyncSender<Box<dyn FnOnce(usize) -> T + Send + 'static>>>§receiver: Arc<Mutex<Receiver<Box<dyn FnOnce(usize) -> T + Send + 'static>>>>§workers: Vec<JoinHandle<()>>§results: Arc<Mutex<Vec<T>>>

Completed task results

§pending: Arc<AtomicUsize>

Number of pending tasks (queued + running)

§pending_pair: Arc<(Mutex<()>, Condvar)>

Pair for wait/notify in join

§size: usize

Implementations§

Source§

impl<T: Send + 'static> ThreadPool<T>

Source

pub fn size(&self) -> usize

Get the number of worker threads in the pool.

Source

pub fn new<'a>( size: usize, name: Option<&'a str>, no_result: bool, ) -> Result<Self, Error>

Create a new thread pool with size workers. The internal submission channel is bounded to size, so when all workers are busy and the channel is full, further submissions will block or return error depending on the flag.

  • name - Optional base name for worker threads. If None, “threadpool-worker-” is used.
  • no_result - If true, results are not stored (saves some overhead if not needed).
Source

pub fn execute<F>( &self, job: F, block_if_full: bool, ) -> Result<(), ExecuteError>
where F: FnOnce(usize) -> T + Send + 'static,

Execute a task. If block_if_full is true, this call will block when the internal submission channel is full (i.e. all workers busy and buffer full) until space becomes available. If block_if_full is false, this returns Err(ExecuteError::Full) when the channel is full.

job: a closure that takes the worker id (0..size-1) and returns a T.

Source

pub fn join(&self)

Wait until all submitted tasks have completed. This does not shut down the pool; new tasks can still be submitted after join returns.

Source

pub fn take_results(&self) -> Vec<T>

Take all results, leaving an empty results vector.

Source

pub fn into_results(self) -> Vec<T>

Wait until all submitted tasks have completed, then return the results.

Trait Implementations§

Source§

impl<T: Send + 'static> Drop for ThreadPool<T>

Source§

fn drop(&mut self)

Executes the destructor for this type. Read more

Auto Trait Implementations§

§

impl<T> Freeze for ThreadPool<T>

§

impl<T> !RefUnwindSafe for ThreadPool<T>

§

impl<T> Send for ThreadPool<T>

§

impl<T> Sync for ThreadPool<T>

§

impl<T> Unpin for ThreadPool<T>

§

impl<T> !UnwindSafe for ThreadPool<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> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
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.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V

Source§

impl<T> ErasedDestructor for T
where T: 'static,