Skip to content

Instantly share code, notes, and snippets.

@qtxie
Forked from Youka/windows_nanosleep.c
Created March 27, 2020 07:53
Show Gist options
  • Save qtxie/877ab6cd1f1f239654ebe28435dd5be2 to your computer and use it in GitHub Desktop.
Save qtxie/877ab6cd1f1f239654ebe28435dd5be2 to your computer and use it in GitHub Desktop.
Windows nanosleep
#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;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment