微秒级延时
windows下Sleep()函数提供毫秒级延时,但是没有微秒级延时,以下代码可以微秒级延时:
void DelayExec(double us)
{
if (us == 0)
{
return;
}
LARGE_INTEGER litmp;
LONGLONG QPart1,QPart2;;
double dfMinus, dfFreq, dfTim;
QueryPerformanceFrequency(&litmp);
dfFreq = (double)litmp.QuadPart; // 获得计数器的时钟频率
QueryPerformanceCounter(&litmp);
QPart1 = litmp.QuadPart; // 获得初始值
do{
QueryPerformanceCounter(&litmp);
QPart2 = litmp.QuadPart;// 获得中止值
dfMinus = (double)(QPart2-QPart1);
dfTim = dfMinus / dfFreq; // 获得对应的时间值,单位为秒
}while(dfTim<(us/1000000));
}