forked from ErickWendel/myownnode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
uv-timers.cpp
55 lines (43 loc) · 1.06 KB
/
uv-timers.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
#include <stdio.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <iostream>
#include <uv.h>
uv_loop_t *loop;
uv_timer_t gc_req;
uv_timer_t fake_job_req;
void callbackFn()
{
printf("callback executed!\n");
}
typedef void (*functionTemplate)(void);
struct timer
{
uv_timer_t req;
std::string text;
functionTemplate *callback;
};
void work(uv_timer_t *handle)
{
timer *timerWrap = (timer *)handle->data;
((functionTemplate)timerWrap->callback)();
printf("%s", timerWrap->text.c_str());
}
int main()
{
loop = uv_default_loop();
for (size_t i = 0; i < 10; i++)
{
timer *timerWrap = new timer();
timerWrap->callback = (functionTemplate *)callbackFn;
timerWrap->text = "hello\n";
timerWrap->req.data = (void *)timerWrap;
// could actually be a TCP download or something
uv_timer_init(loop, &timerWrap->req);
int delay = 500 + i;
int interval = 0;
uv_timer_start(&timerWrap->req, work, delay, interval);
}
return uv_run(loop, UV_RUN_DEFAULT);
}