36 lines
696 B
Rust
36 lines
696 B
Rust
use std::sync::{
|
|
Arc,
|
|
atomic::{AtomicBool, Ordering},
|
|
};
|
|
use std::thread::JoinHandle;
|
|
|
|
pub enum WorkerType {
|
|
MSGRECEPTION,
|
|
MSGSENDER,
|
|
PING,
|
|
MSGRETRY,
|
|
}
|
|
|
|
pub struct Worker {
|
|
thread: Option<JoinHandle<()>>,
|
|
stop: Arc<AtomicBool>,
|
|
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();
|
|
}
|
|
}
|
|
}
|