用QueryPerformanceCounter实现Windows XP 下微秒级延时

windows的Sleep函数,睡眠线程指定毫秒数,可以用来做毫秒延时。

对于微秒延时,没有一个现成的函数,但是可以通过

QueryPerformanceFrequency

QueryPerformanceCounter

来间接实现。原理就是用循环查询的方式不断调用QueryPerformanceCounter(在Winbase.h中)

LARGE_INTEGER freq;
LARGE_INTEGER start, end;


QueryPerformanceFrequency(&freq);

unsigned int us = 100000;
LONGLONG count = (us * freq.QuadPart) / (1000 * 1000);

QueryPerformanceCounter(&start);
   count = count + startQuadPart ;


do
{
   QueryPerformanceCounter(&end);
}while(end.QuadPart< count);

printf("us %u, elaps %u ms\r\n", us, tpend-tpstart);


你可能感兴趣的:(C/C++)