lending_thread_pool

Struct ThreadPool

Source
pub struct ThreadPool<WorkerData: Send + 'static = ()> { /* private fields */ }
Expand description

The Thread Pool struct. This can be constructed using the ThreadPool::new method.

§Examples

use lending_thread_pool::ThreadPool;

let mut pool = ThreadPool::new(
	(0..4)
		.map(|i| format!("Hello from worker {i}"))
		.collect::<Vec<_>>(),
);


for _ in 0..16 {
	pool.enqueue(|greeting| { println!("{greeting}"); });
}

Implementations§

Source§

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

Source

pub fn new(workers_data: Vec<WorkerData>) -> Self

Construct a thread pool given a Vec of WorkerData. The number of workers will correspond to the length of the Vec, so will the queue size for pending tasks. Note that WorkerData can be any type. Each worker thread will own its corresponding WorkerData.

§Panics
  • if Vec is empty
§Examples
use lending_thread_pool::ThreadPool;

let mut pool = ThreadPool::new(
	(0..4)
		.map(|i| format!("Hello from worker {i}"))
		.collect::<Vec<_>>(),
);


for _ in 0..16 {
	pool.enqueue(|greeting| { println!("{greeting}"); });
}
Source

pub fn new_with_queue_size( workers_data: Vec<WorkerData>, max_pending_tasks: usize, ) -> Self

Construct a thread pool given a Vec of WorkerData. The number of workers will correspond to the length of the Vec. Note that WorkerData can be any type you want. Each worker thread will own its corresponding WorkerData.

§Panics
  • if Vec is empty
  • if max_pending_tasks is 0.
§Examples
use lending_thread_pool::ThreadPool;

let mut pool = ThreadPool::new_with_queue_size(
	(0..4)
		.map(|i| format!("Hello from worker {i}"))
		.collect::<Vec<_>>(),
	2,
);


for _ in 0..16 {
	pool.enqueue(|greeting| { println!("{greeting}"); });
}
Source

pub fn enqueue<Task: FnOnce(&mut WorkerData) + Send + 'static>( &mut self, task: Task, )

Enqueue a task in the pool.

§Blocking

This method is blocking. It waits for the task queue to have at least one empty slot before returning.

Source

pub fn join(self)

Signal to all worker threads that they should exit once finished with their current task, then joins all their handles.

Note: join is automatically called on drop.

Trait Implementations§

Source§

impl<WorkerData: Debug + Send + 'static> Debug for ThreadPool<WorkerData>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

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

Source§

fn drop(&mut self)

Executes the destructor for this type. Read more

Auto Trait Implementations§

§

impl<WorkerData> Freeze for ThreadPool<WorkerData>

§

impl<WorkerData = ()> !RefUnwindSafe for ThreadPool<WorkerData>

§

impl<WorkerData> Send for ThreadPool<WorkerData>

§

impl<WorkerData> Sync for ThreadPool<WorkerData>

§

impl<WorkerData> Unpin for ThreadPool<WorkerData>

§

impl<WorkerData = ()> !UnwindSafe for ThreadPool<WorkerData>

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> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
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.
Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more