0

Can anyone please confirm (or deny) whether the steady_clock is "trustworthy" between threads? According to the article Is the epoch of steady_clock relative to when the operating system starts? or to the process itself?, there was some discussion about whether or not the steady_clock was trustworthy at the system level, between processes. My question is, is the steady_clock at least trustworthy between threads?

Weedware
  • 141
  • 1
  • 9

1 Answers1

0

I believe the relevant part of the standard, [time.clock.req], dictates that the clock type dictates the epoch so, as long as you're using steady_clock, threads are irrelevant:

A clock is a bundle consisting of a duration, a time_point, and a function now() to get the current time_point. The origin of the clock’s time_point is referred to as the clock’s epoch.

It would be a poor state of affairs if you couldn't reliably get a duration by subtracting two time_point variables just because one was generated in a separate thread :-)

Note that this only applies within the scope of the standard. Since the question you linked to was to do with separate processes, the standard does not mandate behaviour one way or another.

This is no different to writing a binary time_point (or even an int) and expecting it to be the same when reading it back on a totally different system (for example, writing a little-endian int and trying to read it back on a big-endian system).

But the standard does cover threading so you can safely treat different time_point variable from the same clock type as being compatible.

paxdiablo
  • 854,327
  • 234
  • 1,573
  • 1,953
  • 1
    Poor state of affairs it may or may not be. The problem is that the second answer to the post I provided to the URL to was never countered. And in fact, it was up-voted. So, that said, and let me be more specific: Assuming a thread spawns four threads, and the four threads post specific time_points based on events. And the parent thread has the appropriately synchronized access to those time_points. Can the parent thread trust that the steady_time values posted by each of the four threads represent can be compared to each other? – Weedware Mar 07 '20 at 06:18
  • @Weedware, that answer, and its question, pertain to separate *processes* and the answer is correct because the C++ standard does not care about processes (being outside of its scope means that it does not mandate behaviour). It *does* care a great deal about threads because threads are part of the standard itself. I'll update the answer to hopefully make that clearer. – paxdiablo Mar 07 '20 at 06:28
  • So, in short, the answer to my question was yes. – Weedware Mar 07 '20 at 06:44
  • 1
    In short, yes, it is. I just thought you'd like a little more support than "yes" :-) – paxdiablo Mar 07 '20 at 06:45