-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtwtest.c
63 lines (56 loc) · 1.31 KB
/
twtest.c
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
56
57
58
59
60
61
62
63
#include <unistd.h>
#include <sys/time.h>
#include "timewheel.h"
timewheel_t tw;
void testtask(void *arg)
{
int *data = arg;
struct tm *info;
time_t rawtime;
char buffer[80];
time(&rawtime);
info = localtime(&rawtime);
strftime(buffer, 80, "%Y-%m-%d %H:%M:%S", info);
printf("[%s]testtask: %d\n", buffer, *data);
return;
}
void counttask(void *arg)
{
int *data = arg;
static int count = 0;
struct tm *info;
time_t rawtime;
char buffer[80];
time(&rawtime);
info = localtime(&rawtime);
strftime(buffer, 80, "%Y-%m-%d %H:%M:%S", info);
count += *data;
printf("[%s]counttask: %d\n", buffer, count);
return;
}
int main()
{
tw_init(&tw, TW_TICKSIZE_1MS);
pthread_t twpid = tw_runthread(&tw);
if (twpid == 0) {
printf("Error in creating timewheel clock thread.\n");
return 1;
}
int data0 = 0, data1 = 1, data2 = 2, data3 = 3;
twtask_t *task1, *task2, *task3;
task1 = tw_addtask(&tw, 423, testtask, (void*)&data1);
task2 = tw_addtask(&tw, 900, testtask, (void*)&data2);
tw_settaskperiod(&tw, task2, 1000);
task3 = tw_addtask(&tw, 1700, testtask, (void*)&data3);
tw_settaskperiod(&tw, task3, 777);
testtask((void*)&data0);
sleep(10);
tw_changetask(task2, counttask, (void*)&data1);
while (1) {
sleep(30);
if (task3->flags != TWTASK_FLAG_CANCELLED) {
tw_canceltask(task3);
}
}
return 0;
}