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>
impl<WorkerData: Send + 'static> ThreadPool<WorkerData>
Sourcepub fn new(workers_data: Vec<WorkerData>) -> Self
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}"); });
}Sourcepub fn new_with_queue_size(
workers_data: Vec<WorkerData>,
max_pending_tasks: usize,
) -> Self
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}"); });
}Trait Implementations§
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> 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