我使用libuv库,我想在其他计时器的句柄内运行新的计时器:
void c2(uv_timer_t* handle) {
fprintf(stderr, "c2 timer is executing in event-loop... \n");
}
void c3(uv_timer_t* handle) {
fprintf(stderr, "c3 timer is executing in event-loop... \n");
uv_timer_t timer2;
uv_timer_init(uv_default_loop(), &timer2);
uv_timer_start(&timer2, c2, 5000, 0);
}
int main() {
uv_loop_t *loop = uv_default_loop();
uv_loop_init(loop);
uv_timer_t timer;
uv_timer_init(loop, &timer);
uv_timer_start(&timer, c3, 5000, 0);
uv_run(loop, UV_RUN_DEFAULT);
uv_loop_close(loop);
free(loop);
return 0;
}
我希望在5秒后看到内部控制台:
c3 timer is executing in event-loop...
并在5秒后:
c2 timer is executing in event-loop...
但是我在5秒钟之后看到c3定时器正在执行,并且在5秒之后事件循环刚刚完成工作并且我没有看到c2定时器正在执行。 我不明白为什么以及如何做到这一点?