-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_timer.cc
48 lines (42 loc) · 1 KB
/
test_timer.cc
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
#include "Timer.h"
#include <unistd.h>
#include <time.h>
using namespace common;
void func(std::string& str)
{
printf("timeout %s\n", str.c_str());
}
void timer_cb(int& a, uint64_t start_time)
{
struct timespec end;
clock_gettime(CLOCK_MONOTONIC, &end);
uint64_t end_time = end.tv_sec * 1000000000ULL + end.tv_nsec;
printf("timer_cb %ld\n", (end_time - start_time)/1000000);
a = 0;
}
void* thread_func(void* p)
{
Timer* ptr = (Timer*)p;
int a = 1;
struct timespec start;
clock_gettime(CLOCK_MONOTONIC, &start);
uint64_t start_time = start.tv_sec * 1000000000ULL + start.tv_nsec;
ptr->Add(20000, std::bind(timer_cb, std::ref(a), start_time));
while (a);
ptr->Cancel();
}
int main()
{
Timer t;
pthread_t tid;
pthread_create(&tid, NULL, thread_func, &t);
std::string str("hello 20");
t.Add(20, std::bind(func, str));
str = "hello 22";
t.Add(22, std::bind(func, str));
str = "hello 10000";
t.Add(10000, std::bind(func, str));
t.Run();
pthread_join(tid, NULL);
return 0;
}