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_fullflag:- 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: usizeImplementations§
Source§impl<T: Send + 'static> ThreadPool<T>
impl<T: Send + 'static> ThreadPool<T>
Sourcepub fn new<'a>(
size: usize,
name: Option<&'a str>,
no_result: bool,
) -> Result<Self, Error>
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).
Sourcepub fn execute<F>(
&self,
job: F,
block_if_full: bool,
) -> Result<(), ExecuteError>
pub fn execute<F>( &self, job: F, block_if_full: bool, ) -> Result<(), ExecuteError>
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.
Sourcepub fn join(&self)
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.
Sourcepub fn take_results(&self) -> Vec<T>
pub fn take_results(&self) -> Vec<T>
Take all results, leaving an empty results vector.
Sourcepub fn into_results(self) -> Vec<T>
pub fn into_results(self) -> Vec<T>
Wait until all submitted tasks have completed, then return the results.
Trait Implementations§
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> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
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 moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
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