pub struct ThreadPool { /* private fields */ }Expand description
Thread pool struct.
§Panics
Panics when the generic parameter (POOL_SIZE) is zero.
§Example
use std::thread;
use std::time;
use swimming_pool::ThreadPool;
fn main() {
// Initialize a thread pool with 5 threads
let pool = ThreadPool::new(5);
pool.execute(|| {
// Send job to the pool
thread::sleep(time::Duration::from_secs(5));
});
pool.join_all();
}Implementations§
Source§impl ThreadPool
impl ThreadPool
Sourcepub fn execute<F: FnOnce() + Send + 'static>(&self, f: F)
pub fn execute<F: FnOnce() + Send + 'static>(&self, f: F)
Send a job to the pool. This job will be picked and executed by any free woker thread.
§Example
use threadpool::Threadpool;
let pool = ThreadPool::new(10);
pool.execute(|| {
// Send job to the pool
});Sourcepub fn join_all(self)
pub fn join_all(self)
Join all the worker threads (wait for them to finish executing their job).
Sourcepub fn get_pool_size(&self) -> usize
pub fn get_pool_size(&self) -> usize
Returns the number of worker threads in the thread pool.
Sourcepub fn get_working_threads(&self) -> usize
pub fn get_working_threads(&self) -> usize
Returns the number of woker threads which are currently executing a job.
Auto Trait Implementations§
impl Freeze for ThreadPool
impl !RefUnwindSafe for ThreadPool
impl Send for ThreadPool
impl Sync for ThreadPool
impl Unpin for ThreadPool
impl !UnwindSafe for ThreadPool
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