Advertisement

sleep c++

Started by June 05, 2017 02:38 AM
21 comments, last by Ryan_001 7 years, 5 months ago

For Windows the task scheduler wakes processes up from Sleep at 15.6 ms intervals. Every 15.6 milliseconds it runs through the list and wakes them up in order of priority.  This has been the case for decades, and an enormous body of programs rely on that.

On 6/25/2017 at 10:16 AM, Ryan_001 said:

On my system (Windows 7, Intel i7) both sleep_for() and Sleep() were identical. ... I was getting 1ms accuracy for both (+/- 10us or so).

The only way that should be true is if you happened to sleep for the same amount remaining for the wake interval, or if you (or some other process) altered the system's scheduling frequency.  While it is possible to adjust the scheduler's frequency to 1ms it has serious negative implications, such as greatly increasing power consumption and CPU load as other processes are running 15x more often, and can break a number of other applications that are expecting the standard frequency.

Google Chrome did that for a time, and it had some enormous backlash due to how it broke other systems. Back in late 2012 a creative Chrome developer realized that if he dropped the system's scheduler frequency to 1ms then Chrome would respond much faster to certain events. It was listed as a low-priority bug, until someone did some research that hit global media. Turns out the bug cost about 10 megawatts continuously, hurt laptop battery drain rates by 10%-40% depending on battery type, reduced available CPU processing generally by 2.5% to 5%, and broke a long list of applications.  After enormous backlash they realized that they needed to fix the bug, and finally did so.

If your Sleep on Windows is not waking at a 15.6 millisecond interaval then you need to fix your code so it works with that value.

On 6/25/2017 at 11:16 AM, Ryan_001 said:

With everything closed though that number jumped up and would vary between 5ms and 15ms; but whatever the number was for sleep_for(), Sleep() was identical.  So either VS or some other program in the background was calling timeBeingPeriod(1).

The benchmark wasn't to verify the task scheduler on Windows, it was to determine whether sleep_for() did the same thing as Sleep() or did something different.  I found that at all times, background programs open or closed, irregardless of the sleep amount, that sleep_for() and Sleep() returned identical results.

This topic is closed to new replies.

Advertisement