The multi-second mode of the Sleep() function is well described and well understood. It does nothing unpredictable. Sometimes a function is accused of unpredictability, i.e. When returning before the delay expires. I need to say that this is wrong. A thorough investigation will confirm that his behavior is absolutely predictable. The only problem is that there is a lot to read about it, and most of them are kidding. It is also often said that Windows is not an operating system. But such comments do not give anything, moreover, comments are used to hide the lack of knowledge. It annoys me that not even microsoft notices it and provides better documentation.
However, without exaggerating this small answer: the sleep () function is accurate when used properly and knowing its characteristics. Particular attention should be paid to sleep (0). This is a very powerful tool, especially when used in conjunction with the process priority class, thread priority, media timer settings and processor affinity mask.
Thus, as a rule, true sleep can be performed easily and safely until the period of interruption of the system. When it comes to sleep shorter than the interruption time, spinning is required. A higher resolution time source must be used in another to rotate for shorter periods of time. The most common source of this is a performance counter. QueryPerformanceCounter(*arg) provides incremental * arg. QueryPerformanceFrequency(*arg) provides the frequency at which the performance counter is incremented. Usually it is in MHz mode and depends on the base equipment. The frequency in the MHz range provides microsecond resolution. Thus, to achieve the desired time interval, you can use something with high resolution. However, the accuracy of this should be carefully considered: the OS returns the frequency of the performance counter as a constant. This is not true! Since the frequency is generated as a physical device, there is always a bias, and it is also not constant. It has thermal drift. More modern systems have less drift. But if the thermal drift is only 1 ppm, the error will be 1us / s. The offset can easily be a few 100. An offset of 100 in 1 MHz corresponds to 100US / s.
If a thread needs to wait any high-resolution time, it must establish a service flow. Both threads must have a named event. The service thread should sleep up to 1 interruption period before the desired sleep delay, and then rotate on the performance counter for the remaining microsecond. When a service flow reaches a finite time, it sets up a named event and ends. The calling thread will wake up because it was expecting the named event using the wait function.
Summary:
- Sleep is well understood, but poorly documented.
- Service flow can simulate hibernation with high resolution.
- Such a service thread coulb will be used as a system service.
- The accuracy of the performance counter should be carefully considered. Calibration required.
More information can be found in the Timestamp Windows project.
Arno Jul 13 '12 at 12:33 2012-07-13 12:33
source share