use std::sync::mpsc::{Receiver, Sender}; use threadpool::ThreadPool; /// A multithreaded job pub struct MultithreadedJob { progress: Receiver>, pool: ThreadPool, } impl MultithreadedJob { /// Creates a new multithreaded job pub fn new() -> (Self, Sender>) { let (tx, rx) = std::sync::mpsc::channel(); let pool = ThreadPool::new(6); (Self { progress: rx, pool }, tx) } /// Returns the progress of the job pub fn progress(&self) -> &Receiver> { &self.progress } /// Waits for the job to finish pub fn wait(self) -> Result<(), E> { self.pool.join(); for result in self.progress { result?; } Ok(()) } /// Executes a function on the thread pool pub fn execute(&self, tx: &Sender>, f: F) where F: (FnOnce() -> Result<(), E>) + Send + 'static, { let sender = tx.clone(); self.pool.execute(move || { let result = f(); sender.send(result).unwrap(); }); } }