File tree Expand file tree Collapse file tree 3 files changed +52
-0
lines changed Expand file tree Collapse file tree 3 files changed +52
-0
lines changed Original file line number Diff line number Diff line change @@ -45,6 +45,7 @@ class CppTimer
45
45
**/
46
46
virtual void startns (long nanosecs, cppTimerType_t t = PERIODIC) {
47
47
if (running) return ;
48
+ if (uthread.joinable ()) uthread.join ();
48
49
timerType = t;
49
50
if (fd < 0 )
50
51
throw (" Could not start timer" );
@@ -81,6 +82,7 @@ class CppTimer
81
82
**/
82
83
virtual void startms (long millisecs, cppTimerType_t t = PERIODIC) {
83
84
if (running) return ;
85
+ if (uthread.joinable ()) uthread.join ();
84
86
timerType = t;
85
87
if (fd < 0 )
86
88
throw (" Could not start timer" );
Original file line number Diff line number Diff line change @@ -19,3 +19,7 @@ add_test(TestStartStop_ms test_startstop_ms)
19
19
add_executable (test_twotimers_ms twotimers_ms.cpp )
20
20
TARGET_LINK_LIBRARIES (test_twotimers_ms cpptimer )
21
21
add_test (TestTwotimers_ms test_twotimers_ms )
22
+
23
+ add_executable (test_oneshot oneshot.cpp )
24
+ TARGET_LINK_LIBRARIES (test_oneshot cpptimer )
25
+ add_test (TestOneShot test_oneshot )
Original file line number Diff line number Diff line change
1
+ #include < stdio.h>
2
+ #include " CppTimer.h"
3
+ #include < unistd.h>
4
+ #include < time.h>
5
+ #include < thread>
6
+
7
+ class DemoTimer1 : public CppTimer {
8
+
9
+ public:
10
+ bool hasFired = false ;
11
+ void timerEvent () {
12
+ fprintf (stdout," ." );
13
+ fflush (stdout);
14
+ hasFired = true ;
15
+ }
16
+ };
17
+
18
+
19
+ int main ( int , const char ** ) {
20
+ DemoTimer1 demoTimer1;
21
+ demoTimer1.startns (0.5E9 ,ONESHOT);
22
+
23
+ std::this_thread::sleep_for (std::chrono::seconds (1 ));
24
+
25
+ if (!demoTimer1.hasFired ) {
26
+ char tmp[] = " nanosec Timer has not fired" ;
27
+ printf (" %s\n " ,tmp);
28
+ fflush (stdout);
29
+ throw tmp;
30
+ }
31
+
32
+ demoTimer1.hasFired = false ;
33
+
34
+ demoTimer1.startms (0.5E3 ,ONESHOT);
35
+
36
+ std::this_thread::sleep_for (std::chrono::seconds (1 ));
37
+
38
+ if (!demoTimer1.hasFired ) {
39
+ char tmp[] = " ms timer has not fired" ;
40
+ printf (" %s\n " ,tmp);
41
+ fflush (stdout);
42
+ throw tmp;
43
+ }
44
+
45
+ return 0 ;
46
+ }
You can’t perform that action at this time.
0 commit comments