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

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

    sleep is not very precise in many cases. It depends on the OS how precise. In Windows 7, timer resolution is about 15,4 ms I think. Also, you can usually tell the scheduler how to handle sleep slacking...

    Here is a good read:

    Linux: http://linux.die.net/man/3/nanosleep

    Windows: http://msdn.microsoft.com/en-us/library/ms686298(v=vs.85).aspx

    PS: if you want higher precision on long waits, sleep some period and use the time diff based on a real-time clock. I.e. Store the current time when you start sleeping, then at each interval check how far you are from the set wait time.

提交回复
热议问题