ThreadPool

Struct ThreadPool 

Source
pub struct ThreadPool<Job, Result>
where Job: Send, Result: Send,
{ /* 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>

Source

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 the Send trait.
Source

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.
Source

pub fn receive(&self) -> Result<Result, RecvError>

Receive a result from the worker threads. This function will block until a result is available.

§Returns
  • Result - The result of a job processed by a worker thread.
§Errors
  • If all worker threads panicked, therefore the pipe is closed
Source

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§

Source§

impl<Job: Send, Result: Send> Drop for ThreadPool<Job, Result>

Source§

fn drop(&mut self)

Executes the destructor for this type. Read more

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> 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> 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.