Created
September 8, 2013 07:07
-
-
Save ngryman/6482577 to your computer and use it in GitHub Desktop.
usleep for Windows.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
void usleep(DWORD waitTime){ | |
LARGE_INTEGER perfCnt, start, now; | |
QueryPerformanceFrequency(&perfCnt); | |
QueryPerformanceCounter(&start); | |
do { | |
QueryPerformanceCounter((LARGE_INTEGER*) &now); | |
} while ((now.QuadPart - start.QuadPart) / float(perfCnt.QuadPart) * 1000 * 1000 < waitTime); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
With @latsa's solutions (at least
std::this_thread::sleep_for()
andWaitForSingleObject()
, I'm not using boost), evenusleep(1)
takes at least 10-15ms or so on my Win10 machine, which makes microsecond granularity kinda pointless..Calling
timeBeginPeriod(1);
at the beginning of the program makesSleep(1)
take about 2ms instead of 15ms; with theWaitForSingleObject()
method I can even get down to 1ms, but not below.The busy waiting can be made a bit more CPU-friendly by inserting calls to
_mm_pause()
(that won't decrease the CPU usage that the task manager shows, but the CPU will run at a slighly lower temperature, or at least it did on my machine when inserting like 20 calls to_mm_pause()
in between calls toQueryPerformanceCounter()
)