Linux 具有软实时支持的 C++ 定时器

Linux C++ Timer with Soft Real Time Support

我正在为我的系统编写调度程序,它应该从传感器收集数据。
我的调度程序有计划任务列表。一个线程中的调度程序 运行,单个线程中的 运行 个任务。

请给我推荐支持软实时的 C++ 计时器。
我正在为 vanilla Linux.

编写代码

P.S。我在 Whosebug 上没有发现同样的问题。
P.S.S 抱歉我的英语不好

正如您在评论中阐明的软实时要求

"But i want timer guaranty sleeping time with ms resolution."

从标准 c++ 中,您可以检查实际可用的分辨率,例如std::chrono::high_resolution_clockstd::chrono::system_clock 使用 std::chrono::high_resolution_clock::period 成员类型。如果您当前的系统实现不符合要求的分辨率,您可能会抛出异常等。

这里是 demo 操作方法:

#include <chrono>
#include <ratio>
#include <stdexcept>
#include <iostream>

int main() {
    try {
        // Uncomment any of the following checks for a particular 
        // resolution in question
        if(std::ratio_less_equal<std::nano
          ,std::chrono::system_clock::period>::value) {
        // if(std::ratio_less_equal<std::micro
        //     ,std::chrono::system_clock::period>::value) {
        // if(std::ratio_less_equal<std::milli
        //     ,std::chrono::system_clock::period>::value) {
        // if(std::ratio_less_equal<std::centi
        //     ,std::chrono::system_clock::period>::value) {
            throw std::runtime_error
                ("Clock doesn't meet the actual resolution requirements.");
        }
    }
    catch(const std::exception& ex) {
        std::cout << "Exception: '" << ex.what() << "'" << std::endl;
    }
    std::cout << "The curently available resolution is: " 
              << std::chrono::system_clock::period::num << "/" 
              << std::chrono::system_clock::period::den
              << " seconds" << std::endl;
}

输出(在ideone系统)是:

Exception: 'Clock doesn't meet the actual resolution requirements.'
The curently available resolution is: 1/1000000000 seconds

为了休眠一个预定义的时间段,你可以使用std::thread::sleep_for()实现一个定时器。