Skip to content

Instantly share code, notes, and snippets.

@bitsmanent
Last active December 21, 2022 11:38
Show Gist options
  • Save bitsmanent/be98dca58b40fabb1e72ad41a09ea3e9 to your computer and use it in GitHub Desktop.
Save bitsmanent/be98dca58b40fabb1e72ad41a09ea3e9 to your computer and use it in GitHub Desktop.
Sleep for a specified number of milliseconds
int
msleep(int ms) {
struct timespec req = {0}, rem;
int r = ms / 1000;
if(r >= 1) {
req.tv_sec = r;
ms -= r * 1000;
}
if(ms)
req.tv_nsec = ms * 1000000;
while((r = nanosleep(&req, &rem)) == -1 && errno == EINTR)
req = rem;
return r;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment