1use std::sync::atomic::{AtomicBool, Ordering};
4use std::sync::Arc;
5
6pub struct BackgroundThread {
10 done: Arc<AtomicBool>,
11 thread: Option<std::thread::JoinHandle<()>>,
12}
13
14impl BackgroundThread {
15 pub fn spawn<F: FnOnce(Arc<AtomicBool>) + Send + 'static>(f: F) -> Self {
18 let done = Arc::new(AtomicBool::new(false));
19 let done_p = Arc::clone(&done);
20 let thread = Some(std::thread::spawn(move || f(done_p)));
21 Self { done, thread }
22 }
23
24 pub fn join(self) {
26 }
28}
29
30impl Drop for BackgroundThread {
31 fn drop(&mut self) {
32 self.done.store(true, Ordering::Relaxed);
33 if !std::thread::panicking() {
34 let _ = self.thread.take().unwrap().join();
35 }
36 }
37}