libuv: how to start new timer inside handle of other timer

407 Views Asked by At

I use libuv library and I want to run new timer inside handle of other timer:

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;
}

I expect after 5 seconds to see inside console:

c3 timer is executing in event-loop...

and after 5 seconds later:

c2 timer is executing in event-loop...

but instead that I see after 5 seconds that c3 timer is executing, and after 5 second later event loop just finish work and I do not see that c2 timer is executing. I do not understand why and how can I do it?

0

There are 0 best solutions below