Are longer sleeps (in C++) less precise than short ones

前端 未结 8 1096
情歌与酒
情歌与酒 2021-01-17 09:06

I have a task to do something every \"round\" minute(at xx:xx:00) And I use something like

const int statisticsInterval=60;
    time_t t=0;
    while (1)
            


        
8条回答
  •  感情败类
    2021-01-17 09:50

    If the goal is to sleep until a given system time (xx:xx:00), consider using the overload of boost::this_thread::sleep that takes a time, as in boost::posix_time::ptime, rather than a duration.

    for example,

    #include 
    #include 
    #include 
    int main()
    {
        using namespace boost::posix_time;
        ptime time = boost::get_system_time();
        std::cout << "time is " << time << '\n';
        time_duration tod = time.time_of_day();
        tod = hours(tod.hours()) + minutes(tod.minutes() + 1);
        time = ptime(time.date(), tod);
        std::cout << "sleeping to  " << time << "\n";
        boost::this_thread::sleep(time);
        std::cout << "now the time is " << boost::get_system_time() << '\n';
    }
    

    in C++0x these two overloads were given different names: std::this_thread::sleep_for() and std::this_thread::sleep_until();

提交回复
热议问题