Is the epoch of steady_clock relative to when the operating system starts? or to the process itself?

3.2k views Asked by At

Using boost::chrono::steady_clock or std::chrono::steady_clock is suppose to guarantee that physical time is always monotonic and is not affected by date time changes in the system. Here is my question, if I have two processes that need to be immune to system date time changes, is it enough to exchange just the time_since_epoch? In other words, the time interpretation of the two processes to the same time since epoch will be the same? Specifically I need to answer this question for Windows and QNX.

EDIT: Both processes are running in the same computer, same operating system and communicate via IPC calls.

4

There are 4 answers

0
Some programmer dude On

No the times are not interchangeable between systems, because C++ doesn't specify the epoch. The epoch is depending on the operating system, different systems can have different epochs.

If, on the other hand, you share the times only locally, within the same system, then it's okay.

0
khuttun On

C++ standard says about steady_clock:

20.12.7.2 Class steady_clock [time.clock.steady]

Objects of class steady_clock represent clocks for which values of time_point never decrease as physical time advances and for which values of time_point advance at a steady rate relative to real time. That is, the clock may not be adjusted.

Compare this to what the standard has to say about system_clock:

20.12.7.1 Class system_clock [time.clock.system]

Objects of class system_clock represent wall clock time from the system-wide realtime clock.

There's no mention about steady_clock being "system-wide", which leads me to believe that, according to the C++ standard, you cannot trust on two steady_clocks in different processes on the same machine having the same epoch.

5
DeZee On

Old, but maybe someone will benefit... Probably, as we understood, it isn't defined by the C++ standard, so it depends on your compiler/system/stdlib implementation.

Eg.: If the implementation uses CLOCK_MONOTONIC from Linux (https://man7.org/linux/man-pages/man2/clock_gettime.2.html):

CLOCK_MONOTONIC

A nonsettable system-wide clock that represents monotonic time since—as described by POSIX—"some unspecified point in the past". On Linux, that point corresponds to the number of seconds that the system has been running since it was booted.

It's a system-wide clock then.

0
starriet 주녕차 On

@DeZee mentioned Linux implementation - the epoch is when the system boots.

How about Windows?

MSVC uses QueryPerformanceCounter and QueryPerformanceFrequency for its std::chrono::steady_clock implementation. And from MSDN (emphasis mine):

In general, the performance counter results are consistent across all processors in multi-core and multi-processor systems, even when measured on different threads or processes.

(the doc also mentions some exceptions, so please see it)

And:

The frequency of the performance counter is fixed at system boot and is consistent across all processors so you only need to query the frequency from QueryPerformanceFrequency as the application initializes, and then cache the result.

Also, in the code of the std::chrono::steady_clock implementation of MSVC, we can see this comment:

const long long _Freq = _Query_perf_frequency();    // doesn't change after system boot

Now, your question is:

the time interpretation of the two processes to the same time since epoch will be the same?

So the answer to your question would be "yes", at least for Linux and Windows.