c++ - timerfd 和读取

标签 c++ timer timeout epoll

我有一个应用程序,它会定期(通过计时器)检查一些数据存储。
像这样:

#include <iostream>
#include <cerrno>
#include <cstring>
#include <cstdlib>
#include <sys/fcntl.h>
#include <unistd.h>

// EPOLL & TIMER
#include <sys/epoll.h>
#include <sys/timerfd.h>

int main(int argc, char **argv)
{
    /* epoll instance */
    int efd = epoll_create1(EPOLL_CLOEXEC);

    if (efd < 0)
    {
        std::cerr << "epoll_create error: " << strerror(errno) << std::endl;
        return EXIT_FAILURE;
    }

    struct epoll_event ev;
    struct epoll_event events[128];

    /* timer instance */
    int tfd = timerfd_create(CLOCK_MONOTONIC, TFD_CLOEXEC);

    struct timespec ts;
    // first expiration in 3. seconds after program start
    ts.tv_sec = 3;
    ts.tv_nsec = 0;

    struct itimerspec new_timeout;
    struct itimerspec old_timeout;

    bzero(&new_timeout, sizeof(new_timeout));
    bzero(&old_timeout, sizeof(old_timeout));

    // value
    new_timeout.it_value = ts;

    // no interval;
    // timer will be armed in epoll_wait event trigger
    new_timeout.it_interval.tv_sec =
    new_timeout.it_interval.tv_nsec = 0;

    // Add the timer descriptor to epoll.
    if (tfd != -1)
    {
        ev.events = EPOLLIN | EPOLLERR /*| EPOLLET*/;
        ev.data.ptr = &tfd;
        epoll_ctl(efd, EPOLL_CTL_ADD, tfd, &ev);
    }

    int flags = 0;
    if (timerfd_settime(tfd, flags, &new_timeout, &old_timeout) < 0)
    {
       std::cerr << "timerfd_settime error: " << strerror(errno) << std::endl;
    }

    int numEvents = 0;
    int timeout = 0;
    bool checkTimer = false;
    while (1)
    {
        checkTimer = false;
        numEvents = epoll_wait(efd, events, 128, timeout);
        if (numEvents > 0)
        {
            for (int i = 0; i < numEvents; ++i)
            {
                if (events[i].data.ptr == &tfd)
                {
                    std::cout << "timeout" << std::endl;
                    checkTimer = true;
                }
            }
        }
        else if(numEvents == 0)
        {
            continue;
        }
        else
        {
            std::cerr << "An error occured: " << strerror(errno) << std::endl;
        }

        if (checkTimer)
        {
            /* Check data storage */
            uint64_t value;
            ssize_t readBytes;
            //while ( (readBytes = read(tfd, &value, 8)) > 0)
            //{
            //    std::cout << "\tread: '" << value << "'" << std::endl;
            //}
            itimerspec new_timeout;
            itimerspec old_timeout;
            new_timeout.it_value.tv_sec = rand() % 3 + 1;
            new_timeout.it_value.tv_nsec = 0;
            new_timeout.it_interval.tv_sec =
            new_timeout.it_interval.tv_nsec = 0;
            timerfd_settime(tfd, flags, &new_timeout, &old_timeout);
        }
    }

    return EXIT_SUCCESS;
}

这是对我的应用程序的简单描述。 每次超时后,定时器都需要重新设置一些不同的值。

问题是:

  1. 是否有必要将timerfd添加到带有EPOLLET标志的epoll(epoll_ctl)中?
  2. 是否需要在每次超时后读取timerfd?
  3. 是否需要无限等待epoll_wait(timeout = -1)?

最佳答案

您可以在两种模式之一中执行此操作,边沿触发或电平触发。如果选择边沿触发路由则必须通过EPOLLET,不需要在每次唤醒后读取timerfd。您从 epoll 收到一个事件这一事实意味着一个或多个超时已经触发。您可以选择阅读 timerfd,它会返回 自您上次阅读以来触发的超时次数

如果选择电平触发路由则不需要通过EPOLLET,但是每次唤醒后必须读取timerfd .如果你不这样做,那么你将立即再次被唤醒,直到你耗尽时间。

您应该将 -1 作为超时值或某个正值传递给 epoll。如果您传递 0,就像您在示例中所做的那样,那么您将永远不会进入休眠状态,您只会旋转等待超时触发。这几乎肯定是不受欢迎的行为。

关于c++ - timerfd 和读取,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12491629/

相关文章:

spring - VMware vFabric 服务器超时

http - 在哪里可以找到所有浏览器的默认超时设置?

python - 为什么 python 需要 global 关键字而 C/C++ 不需要?

c++ - 在 Exception 类中使用 unique_ptr 是否安全

c# - 具有不同 TimeSpans 的 .NETMF TimerCallback?

java - 如何自动更改 Jlabel

c++ - 如何让 Cmake 包含 gl/GL.h?

c++ - (int&&)5 是整数常量表达式吗?

java - 坚持设计

networking - TCP Socket 无连接超时