Hacker News new | ask | show | jobs
by nitrogen 5591 days ago
On POSIX systems, clock_nanosleep() can accept an absolute time argument.

  // In a real program one would probably not use time()
  // Also, some older pre-release versions of the realtime
  // kernel patches had the clock_nanosleep() and time() 
  // clocks out of sync by 0.5s (IIRC)
  for(;;) {
    struct timespec after_epoch = { .tv_sec = time(NULL) + 1, .tv_nsec = 0 };
    // Check for EINTR in a real program
    clock_nanosleep(CLOCK_REALTIME, TIMER_ABSTIME, &after_epoch, NULL);
    printf("Tick\n");
  }
http://pubs.opengroup.org/onlinepubs/009695399/functions/clo...

Unfortunately, this probably doesn't help with Flash or JavaScript clock demos.