Created
April 7, 2017 16:42
-
-
Save rafaelglikis/ee7275bf80956a5308af5accb4871135 to your computer and use it in GitHub Desktop.
Cross-platform sleep function for C
This file contains 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
/** | |
* 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