ThreadPool

Struct ThreadPool 

Source
pub struct ThreadPool { /* private fields */ }
Expand description

sender is the Sender end of the channel used for passing tasks to the workers

workers possess threads and are responsible for running the tasks they receiver from the channels in their own threads

Each worker possesses a superficial clone of a single Receiver end that they borrow mutably through parking_lot::Mutex borrow

Implementations§

Source§

impl ThreadPool

Source

pub fn new(size: usize) -> Self

Creates a new ThreadPool with the specified number of worker threads.

The executor service will spawn size worker threads, each of which will process tasks submitted to the service using the execute method.

§Arguments
  • size - The number of worker threads to create.
§Returns

A new FixedThreadPool object.

§Panics

This function will panic if the value of size is equal to zero

§Example
use thread_runner::ThreadPool;

let executor = ThreadPool::new(4);
// execute some tasks
executor.join();
Source

pub fn execute<F: Send + 'static + FnOnce()>(&self, f: F)

Executes the given closure as a task in a worker thread.

This is achieved by sending the task to a pool of workers, who compete to execute it in their threads.

Tasks submitted through the channel are executed in the order they are received (FIFO - First In, First Out). This means if the tasks outnumber the workers, the later tasks are suspended until the earlier tasks are executed.

§Example
use thread_runner::ThreadPool;

let executor = ThreadPool::new(4);

for val in 0..10 {
    executor.execute(move || println!("{}", val));
}

executor.join();
§Note

If you want to wait for the submitted tasks to finish executing, you should call join on the executor service.

Source

pub fn join(self)

Blocks the current thread until the ThreadPool completes all its executions

Source

pub fn terminate(&self)

Auto Trait Implementations§

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