pub fn run_timer_by_state(
step_time: Duration,
run_state: Arc<Mutex<bool>>,
func: Box<dyn Fn()>,
)Expand description
run timer by shared state
use std::sync::{Arc, Mutex};
use doe::*;
let run_state = Arc::new(Mutex::new(true));
let t1 = std::thread::spawn({
let run_state = Arc::clone(&run_state);
move || {
std::thread::sleep(std::time::Duration::from_secs(5));
*run_state.lock().unwrap() = false;
}
});
let t2 = std::thread::spawn({
let run_state = Arc::clone(&run_state);
move || {
run_timer_by_state(
std::time::Duration::from_secs(1),
run_state,
Box::new(|| {
// Function body goes here
println!("Running...");
}),
);
}
});
// Wait for the threads to finish
t1.join().expect("Thread panicked");
t2.join().expect("Thread panicked");