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

前端 未结 8 1085
情歌与酒
情歌与酒 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:45

    When you do sleep(N) it tells the OS to trigger the thread at current time + N.

    The reason why it isn't always accurate, is that you're not the only thread in the system.
    There might be another thread that asked to be waken at that time before you, and there might just be some important OS stuff that's needed to be performed exactly at that time.

    Anyway, there shouldn't be any precision issues, because the method has nothing to do with N.

    The only reason that it won't be "precise" is if it's a crappy OS that can't calculate the time right. And then again, the loop won't solve that.

提交回复
热议问题