1

I am trying to use the clock_gettime() function and I need to use CLOCK_REALTIME since I am trying to synchronize multiple devices. But when I make about 10-20 calls to clock_gettime() I observe that some of them go negative. Why is this happening and what can I do to overcome it? From what I read on the internet one of the reasons for this to occur is because the processor uses multiple cores and the different cores are not synchronized. Is this still the case or could there be some other factors which cause clock_gettime() to return erratic values. I have looked at these clock_gettime() still not monotonic - alternatives? and Linux clock_gettime(CLOCK_MONOTONIC) strange non-monotonic behavior as well as Why is clock_gettime so erratic?. However I cannot use CLOCK_MONOTONIC or CLOCK_MONOTONIC_RAW. I need to use CLOCK_REALTIME. I also turned of NTP sync when perform these experiments and observed that time would still go negative. What else can I do to overcome this problem?

Community
  • 1
  • 1
Developer Android
  • 577
  • 3
  • 5
  • 21
  • 1
    In theory, you could use wall-clock time to do your initial multi-device sync, and then switch to monotonic time to get a steady source (real time X is equal to monotonic time Y; going forward just do diffs). This probably won't work out, because the wall clocks on different devices are unlikely to match precisely, and different clocks aren't going to advance in lock step unless they're actively synchronized. Depending on what it is you're trying to do, you may just have to design your algorithms to work correctly in the face of erratic time sources. – fadden Jul 01 '14 at 04:14

1 Answers1

0

You just can not assume CLOCK_REALTIME to be monotonic or that the wall clock time of your device never jumps forward or backwards.

See this answer for details: Difference between CLOCK_REALTIME and CLOCK_MONOTONIC?

What other solution is viable depends on what problem you are actually trying to solve.

Community
  • 1
  • 1
HHK
  • 4,852
  • 1
  • 23
  • 40