Skip to content

Instantly share code, notes, and snippets.

@savonarola
Created November 12, 2016 21:34
Show Gist options
  • Select an option

  • Save savonarola/c98ae4f6f5265cbf1d3b213e30c55d85 to your computer and use it in GitHub Desktop.

Select an option

Save savonarola/c98ae4f6f5265cbf1d3b213e30c55d85 to your computer and use it in GitHub Desktop.

Revisions

  1. savonarola created this gist Nov 12, 2016.
    30 changes: 30 additions & 0 deletions watchdog.rs
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,30 @@
    use std::{thread, time};

    fn watchdog<F, T>(f: F) -> ()
    where
    F: Send + 'static + Fn() -> T,
    T: Send + 'static
    {
    thread::spawn(move||{
    loop {
    let handle = thread::spawn(f);

    match handle.join() {
    Ok(_) => { println!("thread finished") }
    Err(_) => { println!("thread panicked") }
    }
    }
    });

    }

    fn main() {
    watchdog(
    move||{
    thread::sleep(time::Duration::from_millis(1000));
    }
    );
    thread::sleep(time::Duration::from_millis(10000000));
    }