Skip to content

Instantly share code, notes, and snippets.

@Youka
Created November 22, 2014 13:44
Show Gist options
  • Select an option

  • Save Youka/4153f12cf2e17a77314c to your computer and use it in GitHub Desktop.

Select an option

Save Youka/4153f12cf2e17a77314c to your computer and use it in GitHub Desktop.

Revisions

  1. Youka created this gist Nov 22, 2014.
    23 changes: 23 additions & 0 deletions windows_nanosleep.c
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,23 @@
    #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;
    }