Skip to main content

doe/
timer.rs

1///
2pub mod impl_timer {
3    use std::sync::{Arc, Mutex};
4    /// start_time:Instant,step_time:Duration,stop_time:Duration,func:Box<dyn Fn()>
5    /// ```rust
6    /// use doe::run_timer;
7    /// fn run(){
8    ///     println!("running ");
9    ///}
10    ///   run_timer(std::time::Instant::now(),std::time::Duration::from_secs(1),std::time::Duration::from_secs(5),Box::new(run));
11    ///
12    /// ```
13    pub fn run_timer(
14        start_time: std::time::Instant,
15        step_time: std::time::Duration,
16        stop_time: std::time::Duration,
17        func: Box<dyn Fn()>,
18    ) {
19        let mut elapsed_time = std::time::Duration::new(0, 0);
20
21        while elapsed_time < stop_time {
22            func();
23
24            std::thread::sleep(step_time);
25
26            elapsed_time = start_time.elapsed();
27        }
28    }
29    /// run timer by shared state
30    /// ```rust
31    ///    use std::sync::{Arc, Mutex};
32    ///    use doe::*;
33    ///    let run_state = Arc::new(Mutex::new(true));
34    ///    let t1 = std::thread::spawn({
35    ///        let run_state = Arc::clone(&run_state);
36    ///        move || {
37    ///            std::thread::sleep(std::time::Duration::from_secs(5));
38    ///            *run_state.lock().unwrap() = false;
39    ///        }
40    ///    });
41    ///
42    ///    let t2 = std::thread::spawn({
43    ///        let run_state = Arc::clone(&run_state);
44    ///        move || {
45    ///            run_timer_by_state(
46    ///                std::time::Duration::from_secs(1),
47    ///                run_state,
48    ///                Box::new(|| {
49    ///                    // Function body goes here
50    ///                    println!("Running...");
51    ///                }),
52    ///            );
53    ///        }
54    ///    });
55    ///
56    ///    // Wait for the threads to finish
57    ///    t1.join().expect("Thread panicked");
58    ///    t2.join().expect("Thread panicked");
59    ///
60    /// ```
61    pub fn run_timer_by_state(
62        step_time: std::time::Duration,
63        run_state: Arc<Mutex<bool>>,
64        func: Box<dyn Fn()>,
65    ) {
66        loop {
67            if *run_state.lock().unwrap() {
68                std::thread::sleep(step_time);
69                func();
70            } else {
71                break;
72            }
73        }
74    }
75    use std::sync::mpsc;
76    use std::thread;
77    use std::time::Duration;
78    /// run timer by send stop_massge by channel
79    ///```rust
80    ///use std::sync::mpsc;
81    ///use std::thread;
82    ///use std::time::Duration;
83    ///use doe::*;
84    ///    let (sender, receiver) = mpsc::channel::<bool>();
85    ///    let t1 = thread::spawn(move || {
86    ///        thread::sleep(Duration::from_secs(5));
87    ///        sender.send(true).unwrap();
88    ///    });
89    ///    let t2 = thread::spawn(move || {
90    ///        run_timer_by_channel(Duration::from_secs(1), receiver, Box::new(|| {
91    ///            println!("running..");
92    ///        }));
93    ///    });
94    ///    t1.join().expect("Thread panicked");
95    ///    t2.join().expect("Thread panicked");
96    /// ```
97    pub fn run_timer_by_channel(
98        step_time: Duration,
99        stop_message: mpsc::Receiver<bool>,
100        func: Box<dyn Fn() + Send>,
101    ) {
102        loop {
103            match stop_message.try_recv() {
104                Ok(true) => break,
105                _ => {
106                    thread::sleep(step_time);
107                    func();
108                }
109            }
110        }
111    }
112}