14

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);
quantum
  • 3,672
  • 29
  • 51
payyans4u
  • 351
  • 2
  • 5
  • 16
  • 4
    Define "good". Portable, efficient, well-known, easily understood, accurate, what what what? – unwind Oct 17 '11 at 14:08
  • 1
    Did you read the man pages? http://linux.die.net/man/3/clock_nanosleep – San Jacinto Oct 17 '11 at 14:12
  • @SanJacinto no one reads the man pages. It's like me asking you "Did you read the Facebook terms of service" or a mechanic asking you "Did you even read the user manual" when you can't figure out why your high beams won't work =P – puk Nov 25 '13 at 01:49

2 Answers2

20

The advantages of clock_nanosleep over nanosleep are:

  1. You can specify an absolute time to sleep until rather than an interval to sleep. This makes a difference for the realtime (wall time) clock, which can be reset by the administrator or 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.
  2. You can sleep on timers other than the realtime clock. The most useful is usually the monotonic clock (which can't be reset and increases monotonically with the progression of actual time), but there are also other interesting applications like having one thread in a multi-threaded process sleep on the process's cpu time clock (so it wakes after the process has used a given amount of cpu time).
R.. GitHub STOP HELPING ICE
  • 208,859
  • 35
  • 376
  • 711
  • 3
    Just wanted to add a comment to prevent confusion... nanosleep() in Linux uses the CLOCK_MONOTONIC clock. So if you want to use a clock other than CLOCK_MONOTONIC in Linux, that's when you need clock_nanosleep(). – It'sPete Nov 17 '17 at 23:55
  • @It'sPete: For relative-time sleeps aren't they the same anyway? – R.. GitHub STOP HELPING ICE Dec 16 '19 at 15:47
7

On my system, man 2 clock_nanosleep explains the differences between the two functions thus:

   Like  nanosleep(2), clock_nanosleep() allows the caller to sleep for an
   interval specified with nanosecond precision.  It differs  in  allowing
   the  caller  to select the clock against which the sleep interval is to
   be measured, and in allowing the sleep  interval  to  be  specified  as
   either an absolute or a relative value.

   The clock_id argument [...] can have one of the following values:

   CLOCK_REALTIME   A settable system-wide real-time clock.

   CLOCK_MONOTONIC  A non-settable, monotonically  increasing  clock  that
                    measures time since some unspecified point in the past
                    that does not change after system startup.

   CLOCK_PROCESS_CPUTIME_ID
                    A settable per-process clock that  measures  CPU  time
                    consumed by all threads in the process.

NPE
  • 486,780
  • 108
  • 951
  • 1,012
  • Thanks... I’m using the posix precision sleep standard lib function to sleep, but it appears to be returning far too quickly. Please examine this code and provide details of a way which would repair the function to correctly sleep the correct amount of time. – payyans4u Oct 18 '11 at 03:42