Given the following program -
#include <iostream>
#include <uv.h>
int main()
{
uv_loop_t loop;
uv_loop_init(&loop);
std::cout << "Libuv version: " << UV_VERSION_MAJOR << "."
<< UV_VERSION_MINOR << std::endl;
int r = 0;
uv_timer_t t1_handle;
r = uv_timer_init(&loop, &t1_handle);
uv_timer_start(&t1_handle,
[](uv_timer_t *t) { std::cout << "Timer1 called\n"; }, 0, 2000);
uv_run(&loop, UV_RUN_DEFAULT);
uv_timer_t t2_handle;
r = uv_timer_init(&loop, &t2_handle);
uv_timer_start(&t2_handle,
[](uv_timer_t *t) { std::cout << "Timer2 called\n"; }, 0, 1000);
uv_loop_close(&loop);
}
The second timer descriptor never starts in a loop, because the loop is already running, and "Timer2 called" is never printed. So I tried to temporarily stop the loop after starting, and then add a second timer -
....
uv_run(&loop, UV_RUN_DEFAULT);
// some work
uv_stop(&loop);
// now add second timer
uv_run(&loop, UV_RUN_DEFAULT); // run again
....
But this did not work again, perhaps because later lines will not be executed after the 1st cycle starts with a repeating timer. So, how do I add a new timer descriptor to an already running uvloop?
source
share