Which one of the two functions is better
#include <time.h>
int clock_nanosleep(clockid_t clock_id, int flags, const struct timespec *rqtp, struct timespec *rmtp);
OR
#include <time.h>
int nanosleep(const struct timespec *rqtp, struct timespec *rmtp);
The advantages of clock_nanosleep
over nanosleep
are:
ntpd
, etc. With nanosleep
and precalculating the interval to sleep to reach a given absolute time, you'll fail to wake if the clock is reset and the desired time arrives "early". Also, there's a race condition with scheduling using interval times: If you compute the interval you want to sleep, but you get preempted before calling nanosleep
and don't get scheduled again for a while, you'll again sleep too long.