Struct tinypool::ThreadPool
source · pub struct ThreadPool { /* private fields */ }Expand description
A thread pool that holds a number of threads and executes jobs on them.
Implementations§
source§impl ThreadPool
impl ThreadPool
sourcepub fn new(size: usize) -> Result<Self, ThreadPoolError>
pub fn new(size: usize) -> Result<Self, ThreadPoolError>
Create a new thread pool with the given size.
Arguments
size- The number of threads to create in the thread pool. Use0to determine the number of threads automatically.
Returns
A Result containing the ThreadPool if successful, or a ThreadPoolError if unsuccessful.
Errors
A ThreadPoolError::ThreadSpawn will be returned if the thread pool failed to spawn a thread.
Examples
use tinypool::ThreadPool;
let threadpool = ThreadPool::new(4).unwrap(); // Create a thread pool with 4 threads
let threadpool2 = ThreadPool::new(0).unwrap(); // Create a thread pool with the number of threads determined automaticallysourcepub fn execute<F>(&self, job: F) -> Result<(), ThreadPoolError>where
F: FnOnce() + Send + 'static,
pub fn execute<F>(&self, job: F) -> Result<(), ThreadPoolError>where F: FnOnce() + Send + 'static,
Add a job to the thread pool. Jobs are executed in the order they are added.
Arguments
job- The job (closure) to execute on a worker thread.
Returns
A Result containing () if successful, or a ThreadPoolError if unsuccessful.
Errors
A ThreadPoolError::EmptyPool will be returned if the thread pool is empty.
Examples
use tinypool::ThreadPool;
use std::sync::{Arc, Mutex};
let mut threadpool = ThreadPool::new(4).unwrap();
let counter = Arc::new(Mutex::new(0));
for _ in 0..100 {
let counter_thrd = Arc::clone(&counter);
threadpool.execute(move || {
let mut counter = counter_thrd.lock().unwrap();
*counter += 1;
}).unwrap();
}
threadpool.join();
assert_eq!(*counter.lock().unwrap(), 100);sourcepub fn queued(&self) -> usize
pub fn queued(&self) -> usize
Get the number of queued (and running) jobs.
Returns
The number of queued jobs.
Examples
use tinypool::ThreadPool;
use std::thread;
use std::time::Duration;
let mut threadpool = ThreadPool::new(4).unwrap();
for _ in 0..8 {
threadpool.execute(|| { thread::sleep(Duration::from_secs(1)); }).unwrap();
}
assert_eq!(threadpool.queued(), 8); // 8 jobs (shouldn't have finished yet)
threadpool.join();
assert_eq!(threadpool.queued(), 0); // 0 jobs (should have finished)sourcepub fn set_size(&mut self, size: usize) -> Result<(), ThreadPoolError>
pub fn set_size(&mut self, size: usize) -> Result<(), ThreadPoolError>
Set the number of threads in the thread pool.
If you want to close all threads, use ThreadPool::join() instead.
If reducing the number of threads, this method will close threads only after more jobs aren’t available.
Arguments
size- The number of threads to set the thread pool to. Use0to determine the number of threads automatically.
Returns
A Result containing () if successful, or a ThreadPoolError if unsuccessful.
Errors
A ThreadPoolError::ThreadSpawn will be returned if the thread pool failed to spawn a thread.
Examples
use tinypool::ThreadPool;
use std::thread;
use std::time::Duration;
let mut threadpool = ThreadPool::new(4).unwrap();
assert_eq!(threadpool.size(), 4);
for _ in 0..8 {
threadpool.execute(|| { thread::sleep(Duration::from_secs(1)); }).unwrap();
}
assert_eq!(threadpool.queued(), 8);
threadpool.set_size(8).unwrap();
assert_eq!(threadpool.size(), 8);
assert_eq!(threadpool.queued(), 8); // increasing thread pool size doesn't block main thread
threadpool.set_size(2).unwrap();
assert_eq!(threadpool.size(), 2);
assert!(threadpool.queued() <= 2); // decreasing thread pool size blocks main thread until all jobs are finished
threadpool.join();sourcepub fn join(&mut self)
pub fn join(&mut self)
Wait for all queued jobs to finish and close all threads.
This method will block until all queued jobs have finished.
It will then close all threads in the thread pool.
If you want to wait for all queued jobs to finish, but want to keep the threads running, use ThreadPool::wait() instead.
Examples
use tinypool::ThreadPool;
use std::thread;
use std::time::Duration;
let mut threadpool = ThreadPool::new(4).unwrap();
assert_eq!(threadpool.size(), 4);
assert_eq!(threadpool.queued(), 0);
for _ in 0..8 {
threadpool.execute(|| { thread::sleep(Duration::from_secs(1)); }).unwrap();
}
threadpool.join();
assert_eq!(threadpool.size(), 0);
assert_eq!(threadpool.queued(), 0);sourcepub fn wait(&self)
pub fn wait(&self)
Wait for all queued jobs to finish.
This method will block until all queued jobs have finished.
If you want to wait for all queued jobs to finish and close all threads, use ThreadPool::join() instead.
Examples
use tinypool::ThreadPool;
use std::thread;
use std::time::Duration;
let mut threadpool = ThreadPool::new(4).unwrap();
assert_eq!(threadpool.size(), 4);
assert_eq!(threadpool.queued(), 0);
for _ in 0..8 {
threadpool.execute(|| { thread::sleep(Duration::from_secs(1)); }).unwrap();
}
threadpool.wait();
assert_eq!(threadpool.size(), 4);
assert_eq!(threadpool.queued(), 0);