window sleep function

Reprinted (from  summer solstice Chiaki )

A problem arises

There is such a function in C # and C ++: void  at Sleep (int Timeout), allows threads to suspend specified number of milliseconds. But I call this function under win8 achieve udp packet is sent, there will be a problem according to a fixed frequency. When the left and right Timeout <15ms is, no matter how the Timeout is reduced, a second transmitted data packet and the time Timeout = 15 is substantially similar , except Timeout = 0. [/ (¨Ò o ¨Ò) / ~ ~] So, I began to consider whether or not there is a minimum millisecondsTimeout accuracy.

Second, the survey results are summarized

windows is not a real-time system, about 15ms polls once, check the thread. That it is, with the result that the accuracy of the reasons millisecondsTimeout 15ms it.

The length of different operating system platforms of sleep window is different, win7 sleep (1) 1ms, win10 sleep (1) 14 ~ 15ms.

Third, the solution

There are two ways

①. Multimedia Timer

timeBeginPeriod (1); // set the resolution of 1 millisecond 
:: Sleep (1); // current thread suspend one millisecond 
timeEndPeriod (1); // End precision setting

Such Sleep 1ms accuracy can be raised to a little more (note still fall short 1ms).
Can be used in C # this: "C # multimedia timer to use" http://blog.csdn.net/jane_sl/article/details/8019935
on more multimedia timer: http: //www.cnblogs.com/ liuhao2638 / archive / 2013/06/ 13 / 3134109.html
only custom Sleep function "how do high-precision timer with C #" http://blog.csdn.net/cloudhsu/article/details/5773043

②. Use C ++ 11 new features

std::this_thread::sleep_for(std::chrono::nanoseconds(100));

Reportedly it has been tested and found to run 100,000 data tests found nanoseconds accuracy can only reach about 1.03ms (note still reach the subtle level, nanosecond) but higher than timeBeginPeriod accuracy. I did not test this

Guess you like

Origin www.cnblogs.com/8335IT/p/12370846.html