Struct pconvert_rust::parallelism::ThreadPool
source · pub struct ThreadPool { /* private fields */ }Expand description
Thread pool used in multi-threaded pconvert calls.
Implementations§
source§impl ThreadPool
impl ThreadPool
sourcepub fn new(size: usize) -> Result<ThreadPool, PConvertError>
pub fn new(size: usize) -> Result<ThreadPool, PConvertError>
Creates a thread pool with size worker threads.
sourcepub fn execute<F>(&self, func: F) -> Receiver<ResultMessage>where
F: FnOnce() -> ResultMessage + Send + 'static,
pub fn execute<F>(&self, func: F) -> Receiver<ResultMessage>where F: FnOnce() -> ResultMessage + Send + 'static,
Enqueues a task for execution by any of the worker threads.
Arguments
func- The task to execute.
Return
Returns the receiver end of a channel where the result will be placed.
Examples
use pconvert_rust::parallelism::{ResultMessage, ThreadPool};
use pconvert_rust::utils::read_png_from_file;
let mut thread_pool = ThreadPool::new(10).unwrap();
let path = "path/to/file.png".to_owned();
let demultiply = false;
let result_channel = thread_pool.execute(move || ResultMessage::ImageResult(read_png_from_file(path, demultiply)));
let top = match result_channel.recv().unwrap() {
ResultMessage::ImageResult(result) => result,
}.unwrap();sourcepub fn expand_to(&mut self, num_threads: usize)
pub fn expand_to(&mut self, num_threads: usize)
Expands the thread pool to num_threads.
Creates n workers, where n = num_threads - thread_pool_size.