The runtime-loop crate provides a common runtime loop that continuously attempts to find changes in your system, and act upon them accordingly.
extern crate runtime_loop;
use runtime_loop::{RuntimeLoop, Pollable, Signal};
use std::thread;
use std::sync::{Arc, Mutex};
use std::sync::atomic::Ordering;
use std::time;
let mut rloop = RuntimeLoop::new();
let cancel_flag = rloop.get_cancel_flag();
let target = Arc::new(Mutex::new(Pollable::new(0, Box::new(|x| x+1) ) ) );
if let Ok(mut target) = target.lock() {
target.add_trigger(Signal::new(
Box::new(|x| println!("Increment: {:?}", x))
));
}
rloop.add_pollable(target.clone());
let runtime_thread = thread::spawn(move || {
if let Result::Err(s) = rloop.run() {
panic!(s); }
});
thread::sleep(time::Duration::from_millis(100));
cancel_flag.store(true, Ordering::Relaxed);
if let Result::Err(s) = runtime_thread.join() {
panic!(s);
}