Join background thread and avoid looping forever on windows (#307)
This commit is contained in:
parent
b2ff6d3b56
commit
0c7212511f
@ -273,6 +273,7 @@ impl Iterator for SyncReader {
|
|||||||
pub struct AsyncReader {
|
pub struct AsyncReader {
|
||||||
event_rx: Receiver<InputEvent>,
|
event_rx: Receiver<InputEvent>,
|
||||||
shutdown: Arc<AtomicBool>,
|
shutdown: Arc<AtomicBool>,
|
||||||
|
thread: Option<thread::JoinHandle<()>>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl AsyncReader {
|
impl AsyncReader {
|
||||||
@ -289,13 +290,14 @@ impl AsyncReader {
|
|||||||
let (event_tx, event_rx) = mpsc::channel();
|
let (event_tx, event_rx) = mpsc::channel();
|
||||||
let thread_shutdown = shutdown_handle.clone();
|
let thread_shutdown = shutdown_handle.clone();
|
||||||
|
|
||||||
thread::spawn(move || loop {
|
let thread = thread::spawn(move || {
|
||||||
function(&event_tx, &thread_shutdown);
|
function(&event_tx, &thread_shutdown);
|
||||||
});
|
});
|
||||||
|
|
||||||
AsyncReader {
|
AsyncReader {
|
||||||
event_rx,
|
event_rx,
|
||||||
shutdown: shutdown_handle,
|
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
|
/// * You don't need to call this method, because it will be automatically called when the
|
||||||
/// `AsyncReader` is dropped.
|
/// `AsyncReader` is dropped.
|
||||||
pub fn stop(&mut self) {
|
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");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user