[Holo_wo]-网络编程之timerfd使用说明

Talk is cheap. Show me the code:

#include <assert.h>
#include <cstring>
#include <iostream>
#include <sys/epoll.h>
#include <sys/timerfd.h>
#include <time.h>
#include <unistd.h>
#include <chrono>
#include <vector>
#include <csignal>

void handle_sig(int) {
    std::cout << "bye~" << std::endl;
    exit(0);
}

int main(int argc, char *argv[]) {
    signal(SIGINT, handle_sig);
    int timer_fd = ::timerfd_create(CLOCK_REALTIME, TFD_NONBLOCK);
    struct itimerspec newtimer;
    memset(&newtimer, 0, sizeof(itimerspec));
    struct timespec now;
    clock_gettime(CLOCK_REALTIME, &now);
    std::cout << "timer begin:" << now.tv_sec << std::endl;
    //程序运行后第一次超时时间(用这个作为定时器时间即可)
    newtimer.it_value.tv_nsec = 0;
    newtimer.it_value.tv_sec = 5;
    //之后每次超时间隔(不使用)
    // newtimer.it_interval.tv_nsec = 0;
    // newtimer.it_interval.tv_sec = 5;

    int ret = ::timerfd_settime(timer_fd, 0, &newtimer, nullptr);

    int ep_fd = epoll_create(1); //创建epoll实例对象
    struct epoll_event ev;
    std::vector<struct epoll_event> ready_events(16);
    ev.data.fd = timer_fd;
    ev.events = EPOLLIN | EPOLLET;
    epoll_ctl(ep_fd, EPOLL_CTL_ADD, timer_fd, &ev); //添加到epoll事件集合
    for (;;) {
        int nfd = epoll_wait(ep_fd, &*ready_events.begin(), static_cast<int>(ready_events.size()), 0);
        if (nfd > 0) {
            for (int i = 0; i < nfd; i++) {
                if (ready_events[i].events & EPOLLIN) {
                    if (ready_events[i].data.fd == timer_fd) {
                        //如果不读取数据,之后epoll_wait会阻塞,但是可以采取重新设置的方式来设定超时时间
                        // uint64_t exp;
                        // uint64_t s = read(ready_events[i].data.fd, &exp, sizeof(uint64_t));
                        // assert(s == sizeof(uint64_t));
                        clock_gettime(CLOCK_REALTIME, &now);
                        std::cout << "time:" << now.tv_sec << std::endl;

                        newtimer.it_value.tv_sec = 5;
                        ::timerfd_settime(timer_fd, 0, &newtimer, nullptr);
                    }
                }
            }
        }
        else if (nfd == 0) {
            // nothing happen
        }
        else {
            // error happen
        }
    }
    close(timer_fd);
    close(ep_fd);

    return 0;
}

编译命令:

g++ -std=c++11 timerfd_demo.cpp -o timer_demo -g
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值