libuv 定时器使用

本文介绍了libuv库中的定时器功能,通过查看源码、编译和测试来学习如何使用libuv定时器。文章展示了初始化、设置回调函数和运行的主要步骤,并提醒了uv_timer_start中repeat参数影响回调的重复执行。通过提供的测试代码,读者可以掌握libuv定时器的基本用法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

老大前几天提到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,
             &nb
### LibUV 使用教程及示例代码 #### 1. LibUV 简介 LibUV 是一个跨平台的异步 I/O 库,最初由 Joyent 开发并作为 Node.js 的核心组件之一。它提供了一组强大的 API 来处理事件循环、线程池管理以及文件系统和网络操作等功能。 #### 2. 安装与配置 为了使用 LibUV,首先需要下载其源码并完成编译安装过程。可以通过以下命令获取最新版本的 LibUV 并进行编译: ```bash git clone https://github.com/libuv/libuv.git cd libuv sh autogen.sh ./configure make sudo make install ``` 如果是在 Windows 上开发,则可以按照提供的 QT 工程创建方式,在 `.pro` 文件中指定 `libuv` 的源文件路径[^1]。 #### 3. 示例代码解析 以下是几个常见的 LibUV 功能及其对应的简单示例代码。 ##### (1)基本事件循环 下面展示了如何初始化 LibUV 的事件循环,并运行一个简单的定时器回调函数。 ```c #include <stdio.h> #include <uv.h> void timer_callback(uv_timer_t* handle) { printf("Timer triggered!\n"); } int main() { uv_loop_t *loop = uv_default_loop(); uv_timer_t timer_handle; uv_timer_init(loop, &timer_handle); uv_timer_start(&timer_handle, timer_callback, 1000, 0); uv_run(loop, UV_RUN_DEFAULT); uv_close((uv_handle_t*)&timer_handle, NULL); uv_loop_close(loop); return 0; } ``` 此代码通过设置一个延迟时间为 1 秒的一次性计时器来触发回调函数[^2]。 ##### (2)文件读取 另一个常见场景是对文件执行异步读取操作。以下是从给定文件中读取内容的例子: ```c #include <uv.h> #include <stdio.h> #include <stdlib.h> #include <string.h> uv_loop_t *loop; void on_read(uv_fs_t *req) { if (req->result < 0) { fprintf(stderr, "Read error: %s\n", uv_strerror(req->result)); } else { char *buf = ((uv_buf_t *)req->ptr)->base; size_t len = req->result; printf("File content:\n%.*s\n", (int)len, buf); } uv_fs_req_cleanup(req); uv_stop(loop); } int main() { loop = uv_default_loop(); uv_fs_t req; uv_buf_t buf; char buffer[1024]; buf = uv_buf_init(buffer, sizeof(buffer)); FILE *file = fopen("example.txt", "r+"); if (!file) { perror("fopen failed"); exit(1); } uv_fs_read(loop, &req, (uv_file)fileno(file), &buf, 1, -1, on_read); uv_run(loop, UV_RUN_DEFAULT); fclose(file); uv_loop_close(loop); return 0; } ``` 这段代码实现了打开文件后对其进行异步读取的功能[^4]。 ##### (3)TCP Server 创建 最后,这里给出一个基于 TCP 的服务器端实现例子,该服务监听特定端口等待客户端连接。 ```c #include <stdio.h> #include <stdlib.h> #include <string.h> #include <uv.h> uv_tcp_t server; uv_loop_t *loop; void alloc_buffer(uv_handle_t *handle, size_t suggested_size, uv_buf_t *buf) { (*buf).base = malloc(suggested_size); (*buf).len = suggested_size; } void echo_write(uv_write_t *req, int status) { free(req); } void echo_read(uv_stream_t *client, ssize_t nread, const uv_buf_t *buf) { if (nread < 0) { if (nread != UV_EOF) fprintf(stderr, "Error reading from client socket.\n"); uv_close((uv_handle_t*)client, NULL); free(buf->base); return; } uv_write_t *write_req = malloc(sizeof(*write_req)); uv_buf_t write_buf = uv_buf_init(buf->base, nread); uv_write(write_req, client, &write_buf, 1, echo_write); } void on_new_connection(uv_stream_t *server, int status) { if (status < 0) { fprintf(stderr, "New connection error %s.\n", uv_strerror(status)); return; } uv_tcp_t *client = (uv_tcp_t*)malloc(sizeof(uv_tcp_t)); uv_tcp_init(server->loop, client); if (uv_accept(server, (uv_stream_t*)client) == 0) { uv_read_start((uv_stream_t*)client, alloc_buffer, echo_read); } else { uv_close((uv_handle_t*)client, NULL); } } int main() { loop = uv_default_loop(); uv_ip4_addr("0.0.0.0", 7000, &(struct sockaddr_in){}); uv_tcp_init(loop, &server); uv_bind((uv_stream_t *)&server, (const struct sockaddr *)&addr, 0); int r = uv_listen((uv_stream_t*)&server, SOMAXCONN, on_new_connection); if (r) { fprintf(stderr, "Listen error %s.\n", uv_strerror(r)); return 1; } uv_run(loop, UV_RUN_DEFAULT); uv_loop_close(loop); return 0; } ``` 这个程序建立了一个能够接收消息并将它们回显给发送方的简易 TCP 服务器[^2]。 --- ###
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值