libuv库定时器的使用

老大前几天提到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);

[cpp]  view plain copy
  1. /* uv_timer_test.c */  
  2.   
  3.   
  4. #include   
  5. #include   
  6. #include   
  7. #include   
  8. #include   
  9.   
  10. static void timer_cb(uv_timer_t *handle, int status)   
  11. {  
  12.     static int count;  
  13.     printf("count %d now %d\n", count++, time(NULL));  
  14. }  
  15.   
  16. int main(int argc, char *argv[])  
  17. {  
  18.     int r;  
  19.     uv_timer_t timer;  
  20.     r = uv_timer_init(uv_default_loop(), &timer);  
  21.     assert(r == 0);  
  22.       
  23.     assert(!uv_is_active((uv_handle_t *) &timer));  
  24.     assert(!uv_is_closing((uv_handle_t *) &timer));  
  25.     printf("start %d\n", time(NULL));  
  26.     r = uv_timer_start(&timer, timer_cb, atoi(argv[1]), 0);  
  27.   
  28.     r = uv_run(uv_default_loop(), UV_RUN_DEFAULT);  
  29.     assert(r == 0);  
  30.     return 0;  
  31. }  

测试程序流程非常简单,

初始化定时器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的函数注释基本可以学会使用定时器的使用。

[cpp]  view plain copy
  1. #include   
  2. #include   
  3. #include   
  4. #include   
  5.   
  6. #define ASSERT assert  
  7. static int close_cb_called = 0;  
  8.   
  9.   
  10. static void close_cb(uv_handle_t* handle) {  
  11.   ASSERT(handle != NULL);  
  12.   close_cb_called++;  
  13. }  
  14.   
  15.   
  16. static void timer_cb(uv_timer_t* handle, int status) {  
  17.   ASSERT(0 && "timer_cb should not have been called");  
  18. }  
  19.   
  20.   
  21. int main()  
  22. {  
  23.   int r;  
  24.   uv_timer_t timer;  
  25.   
  26.   r = uv_timer_init(uv_default_loop(), &timer);  
  27.   ASSERT(r == 0);  
  28.   
  29.   ASSERT(!uv_is_active((uv_handle_t*) &timer));  
  30.   ASSERT(!uv_is_closing((uv_handle_t*) &timer));  
  31.   
  32.   r = uv_timer_start(&timer, timer_cb, 1000, 0);  
  33.   ASSERT(r == 0);  
  34.   
  35.   ASSERT(uv_is_active((uv_handle_t*) &timer));  
  36.   ASSERT(!uv_is_closing((uv_handle_t*) &timer));  
  37.   
  38.   r = uv_timer_stop(&timer);  
  39.   ASSERT(r == 0);  
  40.   
  41.   ASSERT(!uv_is_active((uv_handle_t*) &timer));  
  42.   ASSERT(!uv_is_closing((uv_handle_t*) &timer));  
  43.   
  44.   r = uv_timer_start(&timer, timer_cb, 1000, 0);  
  45.   ASSERT(r == 0);  
  46.   
  47.   ASSERT(uv_is_active((uv_handle_t*) &timer));  
  48.   ASSERT(!uv_is_closing((uv_handle_t*) &timer));  
  49.   
  50.   uv_close((uv_handle_t*) &timer, close_cb);  
  51.   
  52.   ASSERT(!uv_is_active((uv_handle_t*) &timer));  
  53.   ASSERT(uv_is_closing((uv_handle_t*) &timer));  
  54.   
  55.   r = uv_run(uv_default_loop(), UV_RUN_DEFAULT);  
  56.   ASSERT(r == 0);  
  57.   
  58.   ASSERT(close_cb_called == 1);  
  59.   
  60.   return 0;  
  61. }

你可能感兴趣的:(Linux)