use std::sync::{ Arc, atomic::{AtomicBool, Ordering}, }; use std::thread::JoinHandle; pub enum WorkerType { MSGRECEPTION, MSGSENDER, PING, MSGRETRY, } pub struct Worker { thread: Option>, stop: Arc, workertype: WorkerType, } impl Worker { pub fn spawn(thread: JoinHandle<()>, workertype: WorkerType) -> Self { Worker { stop: Arc::new(AtomicBool::new(false)), thread: Some(thread), workertype, } } pub fn stop(mut self) { self.stop.store(true, Ordering::Relaxed); if let Some(h) = self.thread.take() { let _ = h.join(); } } }