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);
本文深入探讨了使用Windows Sleep函数实现毫秒级延时,并通过QueryPerformanceCounter间接实现微秒级延时的技术细节。文章详细介绍了延时原理、关键代码解析以及在实际开发中的应用案例。
4046

被折叠的 条评论
为什么被折叠?



