VC自带的SDK中为我们提供了一个Sleep函数,此函数的最小单位为毫秒(既千分之一秒);但在实际的应该中(特别是网络数据传输)我们需要更小的休眠单位(微秒),而系统又没有提供相关API,那么我们如何实现微秒(既十万分之一秒)的休眠呢?
我们知道系统中为我们提供了QueryPerformanceFrequency与QueryPerformanceCounter等相关API,而这些API的时间单位都是微秒及的;这为我们实现微秒休眠提供了思路;为了实用起见我们就直接给出函数代码,代码如下:
03 | void MSleep( long lTime, bool bProcessMsg ) |
06 | LONGLONG QPart1,QPart2; |
07 | double dfMinus, dfFreq, dfTim, dfSpec; |
08 | QueryPerformanceFrequency(&litmp); |
09 | dfFreq = ( double )litmp.QuadPart; |
10 | QueryPerformanceCounter(&litmp); |
11 | QPart1 = litmp.QuadPart; |
12 | dfSpec = 0.000001*lTime; |
16 | if ( bProcessMsg == true ) |
19 | PeekMessage(&msg,NULL,0,0,PM_REMOVE); |
20 | TranslateMessage(&msg); |
21 | DispatchMessage(&msg); |
23 | QueryPerformanceCounter(&litmp); |
24 | QPart2 = litmp.QuadPart; |
25 | dfMinus = ( double )(QPart2-QPart1); |
26 | dfTim = dfMinus / dfFreq; |
http://www.cnblogs.com/rogee/archive/2011/02/15/1954980.html