Skip to content

Instantly share code, notes, and snippets.

@walkanywhere
Forked from Youka/windows_nanosleep.c
Created October 3, 2020 03:41
Show Gist options
  • Select an option

  • Save walkanywhere/62e40bf294990ff37f37e9fb9e7029a3 to your computer and use it in GitHub Desktop.

Select an option

Save walkanywhere/62e40bf294990ff37f37e9fb9e7029a3 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