MFC 中怎么实现微秒级的延时 沉沉_

转自百度文库:

DelayUs
 1 void DelayUs(int uDelay)
 2 {
 3 
 4     LARGE_INTEGER litmp;
 5     LONGLONG QPart1,QPart2;
 6 
 7     double dfMinus,dfFreq,dfTim;
 8     
 9     /*
10         Pointer to a variable that the function sets, in counts per second, to the current performance-counter frequency. 
11         If the installed hardware does not support a high-resolution performance counter, 
12         the value passed back through this pointer can be zero. 
13 
14     */
15     QueryPerformanceFrequency(&litmp);
16 
17     dfFreq = (double)litmp.QuadPart;
18 
19     /*
20         Pointer to a variable that the function sets, in counts, to the current performance-counter value. 
21     */
22     QueryPerformanceCounter(&litmp);
23 
24     QPart1 = litmp.QuadPart;
25     do
26     {
27            QueryPerformanceCounter(&litmp);
28         QPart2 = litmp.QuadPart;
29         dfMinus = (double)(QPart2-QPart1);
30         dfTim = dfMinus/dfFreq;
31      }while(dfTim<0.000001 * uDelay);
32 }

如果你看懂了上述函数,还可以用它里面的部分代码来测试很多函数的执行时间。

当然,这个函数还是有点隐患,如果在一个延时期间,定时器溢出,那么这个值就有可能不准,需要你小心处理。

如:if (dfMinus < 0)

    break;

这个假设硬件定时器是以增加的方式计数的。

原文地址:https://www.cnblogs.com/chenchenluo/p/2721018.html