From 0c7212511fc0139a81ce9983438fe6a20d849587 Mon Sep 17 00:00:00 2001 From: John-John Tedro Date: Mon, 4 Nov 2019 06:50:09 +0100 Subject: [PATCH] Join background thread and avoid looping forever on windows (#307) --- src/input/input/windows.rs | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/src/input/input/windows.rs b/src/input/input/windows.rs index b77a644..c5afbb1 100644 --- a/src/input/input/windows.rs +++ b/src/input/input/windows.rs @@ -273,6 +273,7 @@ impl Iterator for SyncReader { pub struct AsyncReader { event_rx: Receiver, shutdown: Arc, + thread: Option>, } impl AsyncReader { @@ -289,13 +290,14 @@ impl AsyncReader { let (event_tx, event_rx) = mpsc::channel(); let thread_shutdown = shutdown_handle.clone(); - thread::spawn(move || loop { + let thread = thread::spawn(move || { function(&event_tx, &thread_shutdown); }); AsyncReader { event_rx, shutdown: shutdown_handle, + thread: Some(thread), } } @@ -309,7 +311,10 @@ impl AsyncReader { /// * You don't need to call this method, because it will be automatically called when the /// `AsyncReader` is dropped. pub fn stop(&mut self) { - self.shutdown.store(true, Ordering::SeqCst); + if let Some(thread) = self.thread.take() { + self.shutdown.store(true, Ordering::SeqCst); + thread.join().expect("failed to join background thread"); + } } }