pub struct ThreadPool<Job, Result>{ /* private fields */ }Expand description
A thread pool to manage the distribution of jobs to worker threads.
§Template Parameters
Job- The job type that should be processed by the worker threads.Result- The result type that should be returned by the worker threads.
Both Job and Result must implement the Send trait.
Implementations§
Source§impl<Job: Send + JobTrait + 'static, Result: Send + ResultTrait + 'static> ThreadPool<Job, Result>
impl<Job: Send + JobTrait + 'static, Result: Send + ResultTrait + 'static> ThreadPool<Job, Result>
Sourcepub fn new<Argument: Send + 'static>(
args: Vec<Argument>,
func: fn(usize, Job, &Sender<Result>, &Sender<Job>, &mut Argument),
) -> ThreadPool<Job, Result>
pub fn new<Argument: Send + 'static>( args: Vec<Argument>, func: fn(usize, Job, &Sender<Result>, &Sender<Job>, &mut Argument), ) -> ThreadPool<Job, Result>
Create a new thread pool with a given number of worker threads (args.len()).
Each worker thread will receive an argument from the args vector. When a new job
is published to the thread pool, the thread pool will distribute the job to the worker threads
and execute the func function within a worker thread.
§Arguments
args- A vector of arguments that should be passed to the worker threads.func- The worker entry function to process jobs.
§Returns
ThreadPool- The thread pool struct with the worker threads.
§Template Parameters
Argument- The argument type that should be passed to the worker threads. The argument type must implement theSendtrait.
Sourcepub fn publish(&self, job: Job)
pub fn publish(&self, job: Job)
Publish a new job to the thread pool. The job will be distributed to a worker thread.
§Arguments
job- The job that should be processed by a worker thread.
Sourcepub fn receive_timeout(
&self,
timeout: Duration,
) -> Result<Result, RecvTimeoutError>
pub fn receive_timeout( &self, timeout: Duration, ) -> Result<Result, RecvTimeoutError>
Receive a result from the worker threads. This function will block until a result is available or a timeout occurs.
§Arguments
timeout- The maximum time to wait for a result.
§Returns
Result- The result of a job processed by a worker thread.
§Errors
- If all worker threads panicked, therefore the pipe is closed
- If the timeout occurs before a result is available
Trait Implementations§
Auto Trait Implementations§
impl<Job, Result> Freeze for ThreadPool<Job, Result>
impl<Job, Result> !RefUnwindSafe for ThreadPool<Job, Result>
impl<Job, Result> Send for ThreadPool<Job, Result>
impl<Job, Result> !Sync for ThreadPool<Job, Result>
impl<Job, Result> Unpin for ThreadPool<Job, Result>
impl<Job, Result> !UnwindSafe for ThreadPool<Job, Result>
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
Mutably borrows from an owned value. Read more