libuv定时器的使用
2017-07-27 00:00
519 查看
定时器有这么以下几个函数:
第一个uv_loop_t参数即用uv_loop_new()创建.
特别注意的是handle不能是空指针,即不能定义一个指针而不申请内存传入.
cb: 即回调函数, 函数声明是这样的:
其中的handle即为到定时时间的定时器句柄.
timeout: 第一次调用timer的超时时间;
repeat: 重复调用cb回调的周期;
int uv_timer_init(uv_loop_t *, uv_timer_t *handle); int uv_timer_start(uv_timer_t *handle, uv_timer_cb cb, uint64_t timeout, uint64_t repeat); int uv_timer_stop(uv_timer_t *handle); int uv_timer_again(uv_timer_t *handle); void uv_timer_set_repeat(uv_timer_t *handle, uint64_t repeat); uint64_t uv_timer_get_repeat(const uv_timer_t *handle);
uv_timer_init
uv_timer_init主要是将uv_timer_t绑定到uv_loop_t.
第一个uv_loop_t参数即用uv_loop_new()创建.
特别注意的是handle不能是空指针,即不能定义一个指针而不申请内存传入.
uv_timer_start
handle 定时器句柄,由uv_timer_init初始化;cb: 即回调函数, 函数声明是这样的:
typedef void (*uv_timer_cb)(uv_timer_t *handle);
其中的handle即为到定时时间的定时器句柄.
timeout: 第一次调用timer的超时时间;
repeat: 重复调用cb回调的周期;
uv_timer_stop
停止定时器uv_timer_again
一般配合uv_timer_stop使用.uv_timer_set_repeat/uv_timer_get_repeat
设置/获取重载时间.测试用例
#include <stdio.h> #include <stdlib.h> #include <time.h> static int i = 0; void timer_cb(uv_timer_t *handle) { printf("timer_cb: %lu\n", time(nullptr)); if(++i >= 5) uv_timer_stop(handle); // 这里调用,便会停止定时器,uvLoop无克执行任务后,会导致main自动退出. } int main(int argc, char *argv[]) { uv_loop_t *uvLoop = uv_loop_new(); uv_timer_t uvTimer; printf("Main start!\n"); uv_timer_init(uvLoop, &uvTimer); uv_timer_start(&uvTimer, timer_cb, 0, 1000); uv_run(uvLoop, UV_RUN_DEFAULT); printf("Main end!\n"); return 0; }
相关文章推荐
- libuv 定时器使用
- libuv 之定时器的使用
- iOS项目开发实战——使用CALayer和定时器实现进度条
- 使用定时器T1的模式2 定时器T0的模式2 每隔两秒左右切换指示灯
- TI-RTOS 定时器的使用
- spring中quartz定时器的使用
- 使用单片机的定时器0和中断实现9到0的倒计时功能,并用数码管显示
- 定时器的几种使用方式。
- C++的console使用windows的消息机制,定时器等
- Qt定时器的使用
- spring框架中定时器的使用和配置
- 【编码随笔】Win32下定时器使用解释与示例
- jquery计算时间差(天、时、分、秒)并使用定时器实时获取
- Java Timer 定时器的使用
- Spring框架中的定时器 如何使用和配置
- 定时器的使用方法
- Java 定时器的使用
- 使用定时器显示GIF动画的ATL控件实现
- VC++实现非窗口类中使用定时器的方法
- 如何使用 systemd 中的定时器