Skip to content

Instantly share code, notes, and snippets.

@rafaelglikis
Created April 7, 2017 16:42
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rafaelglikis/ee7275bf80956a5308af5accb4871135 to your computer and use it in GitHub Desktop.
Save rafaelglikis/ee7275bf80956a5308af5accb4871135 to your computer and use it in GitHub Desktop.
Cross-platform sleep function for C
/**
* Cross-platform sleep function for C
* @param int milliseconds
*/
void sleep_ms(int milliseconds)
{
#ifdef WIN32
Sleep(milliseconds);
#elif _POSIX_C_SOURCE >= 199309L
struct timespec ts;
ts.tv_sec = milliseconds / 1000;
ts.tv_nsec = (milliseconds % 1000) * 1000000;
nanosleep(&ts, NULL);
#else
usleep(milliseconds * 1000);
#endif
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment