libuv 定时器使用

时间:2020-12-04 00:22:49

老大前几天提到libuv,我只看过一点libevent, 听说过libev,没听说过libuv,查了一下才知道libuv。

先到github git clone 源码,编译,测试,看测试代码


头文件uv.h定时器部分的函数,头文件有详细的注释

/*
 * uv_timer_t is a subclass of uv_handle_t.
 *
 * Used to get woken up at a specified time in the future.
 */
struct uv_timer_s {
  UV_HANDLE_FIELDS
  UV_TIMER_PRIVATE_FIELDS
};

UV_EXTERN int uv_timer_init(uv_loop_t*, uv_timer_t* handle);

/*
 * Start the timer. `timeout` and `repeat` are in milliseconds.
 *
 * If timeout is zero, the callback fires on the next tick of the event loop.
 *
 * If repeat is non-zero, the callback fires first after timeout milliseconds
 * and then repeatedly after repeat milliseconds.
 */
UV_EXTERN int uv_timer_start(uv_timer_t* handle,
                             uv_timer_cb cb,
                             uint64_t timeout,
                             uint64_t repeat);

UV_EXTERN int uv_timer_stop(uv_timer_t* handle);

/*
 * Stop the timer, and if it is repeating restart it using the repeat value
 * as the timeout. If the timer has never been started before it returns -1 and
 * sets the error to UV_EINVAL.
 */
UV_EXTERN int uv_timer_again(uv_timer_t* handle);

/*
 * Set the repeat value in milliseconds. Note that if the repeat value is set
 * from a timer callback it does not immediately take effect. If the timer was
 * non-repeating before, it will have been stopped. If it was repeating, then
 * the old repeat value will have been used to schedule the next timeout.
 */
UV_EXTERN void uv_timer_set_repeat(uv_timer_t* handle, uint64_t repeat);

UV_EXTERN uint64_t uv_timer_get_repeat(const uv_timer_t* handle);

/* uv_timer_test.c */


#include <stdlib.h>
#include <uv.h>
#include <stdio.h>
#include <assert.h>
#include <time.h>

static void timer_cb(uv_timer_t *handle, int status)
{
static int count;
printf("count %d now %d\n", count++, time(NULL));
}

int main(int argc, char *argv[])
{
int r;
uv_timer_t timer;
r = uv_timer_init(uv_default_loop(), &timer);
assert(r == 0);

assert(!uv_is_active((uv_handle_t *) &timer));
assert(!uv_is_closing((uv_handle_t *) &timer));
printf("start %d\n", time(NULL));
r = uv_timer_start(&timer, timer_cb, atoi(argv[1]), 0);

r = uv_run(uv_default_loop(), UV_RUN_DEFAULT);
assert(r == 0);
return 0;
}

测试程序流程非常简单,

初始化定时器uv_timer_init

注册自己的定时回调函数uv_timer_start

运行 uv_run


编译:gcc -o test-uv-timer test_uv_timer.c -luv


注意在uv_timer_start函数的参数repeat设置为1的话,time_cb在第一次定时时间到后之后会一直回调


最后贴上修改的test-acitive.c代码,从这个测试代码和uv.h的函数注释基本可以学会使用定时器的使用。

#include <uv.h>
#include <assert.h>
#include <stdio.h>
#include <stdlib.h>

#define ASSERT assert
static int close_cb_called = 0;


static void close_cb(uv_handle_t* handle) {
ASSERT(handle != NULL);
close_cb_called++;
}


static void timer_cb(uv_timer_t* handle, int status) {
ASSERT(0 && "timer_cb should not have been called");
}


int main()
{
int r;
uv_timer_t timer;

r = uv_timer_init(uv_default_loop(), &timer);
ASSERT(r == 0);

ASSERT(!uv_is_active((uv_handle_t*) &timer));
ASSERT(!uv_is_closing((uv_handle_t*) &timer));

r = uv_timer_start(&timer, timer_cb, 1000, 0);
ASSERT(r == 0);

ASSERT(uv_is_active((uv_handle_t*) &timer));
ASSERT(!uv_is_closing((uv_handle_t*) &timer));

r = uv_timer_stop(&timer);
ASSERT(r == 0);

ASSERT(!uv_is_active((uv_handle_t*) &timer));
ASSERT(!uv_is_closing((uv_handle_t*) &timer));

r = uv_timer_start(&timer, timer_cb, 1000, 0);
ASSERT(r == 0);

ASSERT(uv_is_active((uv_handle_t*) &timer));
ASSERT(!uv_is_closing((uv_handle_t*) &timer));

uv_close((uv_handle_t*) &timer, close_cb);

ASSERT(!uv_is_active((uv_handle_t*) &timer));
ASSERT(uv_is_closing((uv_handle_t*) &timer));

r = uv_run(uv_default_loop(), UV_RUN_DEFAULT);
ASSERT(r == 0);

ASSERT(close_cb_called == 1);

return 0;
}