6
考虑下面的程序 -再添计时器上已经运行循环
#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);
// second timer
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);
}
第二个定时器手柄是从来没有在循环中运行,由于循环已经在运行,而“定时器称为”从不打印。所以,我想运行它,然后将第二定时器后暂时停止循环 -
....
uv_run(&loop, UV_RUN_DEFAULT);
// some work
uv_stop(&loop);
// now add second timer
uv_run(&loop, UV_RUN_DEFAULT); // run again
....
但是这一次第一环开始与重复运行后没有工作,可能是因为后来的行不会被执行计时器。那么我应该如何为已经运行的uvloop添加一个新的定时器句柄?
凉爽,所以我们可以操纵循环中它的回调函数,只是确保我们首先停止它。 –