use crossbeam_channel::{unbounded, Receiver, Sender};
use std::thread::{self, JoinHandle};
pub struct WorkerThread {
handle: JoinHandle<()>,
done: Sender<()>,
}
impl WorkerThread {
pub fn spawn<F>(fun: F) -> Self
where
F: FnOnce(Receiver<()>) + Send + 'static,
{
let (tx, rx) = unbounded::<()>();
let handle = thread::spawn(move || fun(rx));
Self { handle, done: tx }
}
pub fn close(self) {
self.done.send(()).unwrap();
self.handle.join().unwrap();
}
}