Note
This post is for GNU/Linux and POSIX systems.
It’s quite often when I work on an old ANSI C/C89 code, if it’s using usleep and -Wall, then there would be a warning:
warning: implicit declaration of function 'usleep'
Since GNU C Library 2.12 (May 2010), you need to have right macros to have it, see usleep(3). I think the better way is to move onto nanosleep if it’s for POSIX systems, and every time I would have to copy from previous modified code. I think I will make a note for it.
1 usleep
usleep(SLEEP);
2 nanosleep
#define _POSIX_C_SOURCE 199309L #include <time.h> struct timespec req = { .tv_sec = SLEEP / 1000000, .tv_nsec = SLEEP * 1000 }; nanosleep(&req, NULL);
Note that if SLEEP is for less than a second, then you can directly set .tv_sec to 0. Also _POSIX_C_SOURCE >= 199309L is required.
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.