Created
November 22, 2014 13:44
-
-
Save Youka/4153f12cf2e17a77314c to your computer and use it in GitHub Desktop.
Windows nanosleep
This file contains hidden or 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
#include <windows.h> /* WinAPI */ | |
/* Windows sleep in 100ns units */ | |
BOOLEAN nanosleep(LONGLONG ns){ | |
/* Declarations */ | |
HANDLE timer; /* Timer handle */ | |
LARGE_INTEGER li; /* Time defintion */ | |
/* Create timer */ | |
if(!(timer = CreateWaitableTimer(NULL, TRUE, NULL))) | |
return FALSE; | |
/* Set timer properties */ | |
li.QuadPart = -ns; | |
if(!SetWaitableTimer(timer, &li, 0, NULL, NULL, FALSE)){ | |
CloseHandle(timer); | |
return FALSE; | |
} | |
/* Start & wait for timer */ | |
WaitForSingleObject(timer, INFINITE); | |
/* Clean resources */ | |
CloseHandle(timer); | |
/* Slept without problems */ | |
return TRUE; | |
} |
This solution works like a charm for me. Thanks! Also note that this solution has been posted here: https://stackoverflow.com/a/41862592/11550178. Actually, this is an official solution extracted from here: https://learn.microsoft.com/en-us/windows/win32/sync/using-waitable-timer-objects.
Show me the timings please so I know
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This solution works like a charm for me. Thanks! Also note that this solution has been posted here: https://stackoverflow.com/a/41862592/11550178. Actually, this is an official solution extracted from here: https://learn.microsoft.com/en-us/windows/win32/sync/using-waitable-timer-objects.