From 4186c025fa84bd6ea7a0ddcf2c869e746de90c51 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Simon=20W=C3=B6rner?= Date: Fri, 31 Mar 2017 12:38:36 +0200 Subject: [PATCH] fixed signale handling --- V1/led5/src/main.rs | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/V1/led5/src/main.rs b/V1/led5/src/main.rs index 07dab86..69d589a 100644 --- a/V1/led5/src/main.rs +++ b/V1/led5/src/main.rs @@ -5,6 +5,7 @@ extern crate chan_signal; use std::io::prelude::*; use std::fs::File; use std::thread; +use std::thread::JoinHandle; use std::path::Path; use std::time::Duration; @@ -67,15 +68,18 @@ fn get_value(port: &str) -> String { fn main() { let signal = chan_signal::notify(&[Signal::INT, Signal::TERM]); let (sdone, rdone) = chan::sync(0); + let (spanic, rpanic) = chan::sync(0); let worker = thread::Builder::new() .name("worker".to_string()) .spawn(move || run(rdone)) .expect("Create worker thread failed"); + let watchdog = thread::Builder::new() + .name("watchdog".to_string()) + .spawn(move || watchdog(worker, spanic)) + .expect("Create watchdog thread failed"); chan_select! { - default => { - worker.join().expect("Worker thread paniced"); - }, + rpanic.recv() => { }, signal.recv() -> signal => { #[cfg(debug_assertions)] println!("received signal: {:?}", signal); @@ -83,6 +87,12 @@ fn main() { sdone.send(()); } } + + watchdog.join().expect("Watchdog thread paniced"); +} + +fn watchdog(thread: JoinHandle<()>, sdone: chan::Sender<()>) { + thread.join().expect("Thread paniced"); } fn run(rdone: chan::Receiver<()>) {