-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
TimerThread_test.cpp
55 lines (42 loc) · 1.39 KB
/
TimerThread_test.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
/*
* TimerThread_test.cpp
*
* @build: make evpp
*
*/
#include "TimerThread.h"
#include "singleton.h"
namespace hv {
class GlobalTimerThread : public TimerThread {
SINGLETON_DECL(GlobalTimerThread)
protected:
GlobalTimerThread() : TimerThread() {}
~GlobalTimerThread() {}
public:
static TimerID setTimeout(int timeout_ms, TimerCallback cb) {
return GlobalTimerThread::instance()->setTimer(timeout_ms, cb, 1);
}
static void clearTimeout(TimerID timerID) {
GlobalTimerThread::instance()->killTimer(timerID);
}
static TimerID setInterval(int interval_ms, TimerCallback cb) {
return GlobalTimerThread::instance()->setTimer(interval_ms, cb, INFINITE);
}
static void clearInterval(TimerID timerID) {
GlobalTimerThread::instance()->killTimer(timerID);
}
};
SINGLETON_IMPL(GlobalTimerThread)
} // end namespace hv
int main(int argc, char* argv[]) {
hv::GlobalTimerThread::setTimeout(3000, [](hv::TimerID timerID) {
printf("setTimeout timerID=%lu time=%lus\n", (unsigned long)timerID, (unsigned long)time(NULL));
});
hv::GlobalTimerThread::setInterval(1000, [](hv::TimerID timerID) {
printf("setInterval timerID=%lu time=%lus\n", (unsigned long)timerID, (unsigned long)time(NULL));
});
// press Enter to stop
while (getchar() != '\n');
hv::GlobalTimerThread::exitInstance();
return 0;
}