Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/mercurial/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 没有信号处理器的POSIX定时器的用途是什么?_C++_Linux - Fatal编程技术网

C++ 没有信号处理器的POSIX定时器的用途是什么?

C++ 没有信号处理器的POSIX定时器的用途是什么?,c++,linux,C++,Linux,此函数应一直阻止,直到m_epoll_fd上出现事件。您可以看到,有一个计时器m_timer_fd,它是创建的,但从未提供过处理程序。启动没有处理程序的计时器有什么意义 另外,我在其他地方看不到这个计时器的任何用法。因为它是在本地创建的,所以在它的类中没有任何用途。它必须有本地用法 功能取自强调矿井的: 这些系统调用创建并操作一个传递计时器的计时器 通过文件描述符发出过期通知。它们提供了一个 除了使用setitimer2或timer_create2之外,还可以使用 优点是可以通过select2监

此函数应一直阻止,直到m_epoll_fd上出现事件。您可以看到,有一个计时器m_timer_fd,它是创建的,但从未提供过处理程序。启动没有处理程序的计时器有什么意义

另外,我在其他地方看不到这个计时器的任何用法。因为它是在本地创建的,所以在它的类中没有任何用途。它必须有本地用法

功能取自强调矿井的

这些系统调用创建并操作一个传递计时器的计时器 通过文件描述符发出过期通知。它们提供了一个 除了使用setitimer2或timer_create2之外,还可以使用 优点是可以通过select2监控文件描述符, poll2和epoll7

从原始源代码:

void EventProviderLinux::waitForEvents (EventLoopTime wait_time)
{
    AIPSTACK_ASSERT(m_cur_epoll_event == m_num_epoll_events);

    namespace chrono = std::chrono;
    using Period = EventLoopTime::period;
    using Rep = EventLoopTime::rep;
    using SecType = decltype(itimerspec().it_value.tv_sec);
    using NsecType = decltype(itimerspec().it_value.tv_nsec);
    using NsecDuration = chrono::duration<NsecType, std::nano>;

    static_assert(Period::num == 1);
    static_assert(Period::den <= std::nano::den);
    static_assert(std::is_signed_v<Rep>);
    static_assert(std::is_signed_v<SecType>);
    static_assert(TypeMax<Rep> / Period::den <= TypeMax<SecType>);
    static_assert(TypeMin<Rep> / Period::den >= TypeMin<SecType> + 1);

    if (wait_time != m_timerfd_time || m_force_timerfd_update) {
        m_force_timerfd_update = true;

        EventLoopTime::duration time_dur = wait_time.time_since_epoch();

        SecType sec = time_dur.count() / Period::den;
        Rep subsec = time_dur.count() % Period::den;
        if (subsec < 0) {
            sec--;
            subsec += Period::den;
        }

        struct itimerspec itspec = {};
        itspec.it_value.tv_sec = sec;
        itspec.it_value.tv_nsec =
            chrono::duration_cast<NsecDuration>(EventLoopTime::duration(subsec)).count();

        // Prevent accidentally disarming the timerfd.
        if (itspec.it_value.tv_sec == 0 && itspec.it_value.tv_nsec == 0) {
            itspec.it_value.tv_nsec = 1;
        }

        if (::timerfd_settime(*m_timer_fd, TFD_TIMER_ABSTIME, &itspec, nullptr) < 0) {
            throw std::runtime_error(formatString(
                "EventProviderLinux: timerfd_settime failed, err=%d", errno));
        }

        m_timerfd_time = wait_time;
        m_force_timerfd_update = false;
    }

    int wait_res;
    while (true) {
        wait_res = ::epoll_wait(*m_epoll_fd, m_epoll_events, MaxEpollEvents, -1);
        if (AIPSTACK_LIKELY(wait_res >= 0)) {
            break;
        }

        int err = errno;
        if (err != EINTR) {
            throw std::runtime_error(formatString(
                "EventProviderLinux: epoll_wait failed, err=%d", err));
        }
    }

    AIPSTACK_ASSERT(wait_res <= MaxEpollEvents);

    m_cur_epoll_event = 0;
    m_num_epoll_events = wait_res;
}

从上面我们可以看到,计时器事件已设置为通过epoll监控。

计时器由epoll监控
control_epoll(EPOLL_CTL_ADD, *m_timer_fd, EPOLLIN, &m_timer_fd);