VC sleep in microseconds (100,000 in one second)

Source: Internet
Author: User
Tags sleep function
The SDK provided by VC provides us with a Sleep function. The minimum unit of this function is Millisecond (1‰ s). However, in practice (especially network data transmission) we need a smaller sleep unit (microseconds), and the system does not provide relevant APIs. How can we achieve sleep in microseconds (one thousandth second?

We know that the system provides QueryPerformanceFrequency, QueryPerformanceCounter, and other related APIs. The time units of these APIs are in microseconds. This provides us with an idea to implement microsecond sleep; for the sake of practicality, the function code is provided directly. The Code is as follows:

// LTime ---- sleep time (microseconds) // bProcessMsg ---- whether to process system message void MSleep (long lTime, bool bProcessMsg) {LARGE_INTEGER litmp; LONGLONG QPart1, QPart2; double dfMinus, dfFreq, dfTim, dfSpec; QueryPerformanceFrequency (& litmp); dfFreq = (double) litmp. quadPart; QueryPerformanceCounter (& litmp); QPart1 = litmp. quadPart; dfSpec = 0.000001 * lTime; do {if (bProcessMsg = true) {MSG msg; PeekMessage (& msg, NULL, PM_REMOVE); TranslateMessage (& msg ); dispatchMessage (& msg);} QueryPerformanceCounter (& litmp); QPart2 = litmp. quadPart; dfMinus = (double) (QPart2-QPart1); dfTim = dfMinus/dfFreq;} while (dfTim <dfSpec );}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.