windows的Sleep函數,睡眠線程指定毫秒數,能夠用來作毫秒延時。 對於微秒延時,沒有一個現成的函數,可是能夠經過 QueryPerformanceFrequency QueryPerformanceCounter 來間接實現。原理就是用循環查詢的方式不斷調用QueryPerformanceCounter(在Winbase.h中)windows
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);```