timerfd_settime()的第二个参数?

想请教大家,我想使用 Linux 下定时器 timerfd

示例如下:

    struct itimerspec new_value;
    struct timespec now;

    int ret = clock_gettime(CLOCK_REALTIME, &now);//获取时钟时间
    assert(ret != -1);


    new_value.it_value.tv_sec = now.tv_sec + 1; //第一次到期的时间
    new_value.it_value.tv_nsec = now.tv_nsec; 

    new_value.it_interval.tv_sec = 1;      //之后每次到期的时间间隔
    new_value.it_interval.tv_nsec = 0;

    int timefd = timerfd_create(CLOCK_REALTIME, TFD_NONBLOCK); // 构建了一个定时器
    assert(timefd != -1);

    // ret = timerfd_settime(timefd, `0, &new_value, NULL);//启动定时器
    ret = timerfd_settime(timefd, TFD_TIMER_ABSTIME, &new_value, NULL);//启动定时器

如上例, 我想使用参数CLOCK_REALTIME创建一个相对时间定时器, 在clock_gettimetimerfd_create函数中均使用了该参数, 然后在timerfd_settime函数中第二个参数为0时不应该是对应的相对定会器么, 但是设置为0时使用epoll去监听时时并没有触发定时器, 反而是在timerfd_settime第二个参数设置为TFD_TIMER_ABSTIME时定时器正常工作 (这个参数不应该对应的是绝对时间么)

阅读 5.1k
1 个回答

timerfd man page

By default, the initial expiration time specified in new_value is interpreted relative to the current time on the timer's clock at the time of the call (i.e., new_value.it_value specifies a time relative to the current value of the clock specified by clockid).

new_value.it_value设置初次定时的时间

如果timerfd_settime第二个参数设置为0,new_value.it_value设置为1
如果timerfd_settime第二个参数设置为TFD_TIMER_ABSTIME,new_value.it_value设置为now.tv_sec + 1

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题