Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a standard library implementation where high_resolution_clock is not a typedef?

Tags:

c++

chrono

The C++ Draft par 20.12.7.3 reads:

high_resolution_clock may be a synonym for system_clock or steady_clock

Of course this may mandates nothing but I wonder :

  • Is there any point for high_resolution_clock to something other that a typedef ?
  • Are there such implementations ?
  • If a clock with a shorter tick period is devised it can either be steady or not steady. So if such a mechanism exists, wouldn't we want to "improve" system_clock and high_resolution_clock as well, defaulting to the typedef solution once more ?
like image 874
Lorah Attkins Avatar asked Feb 28 '16 12:02

Lorah Attkins


1 Answers

The reason that specs have wording such as "may" and "can", and other vague words that allow for other possibilities comes from the wish by the spec writers not wanting to (unnecessarily) limit the implementation of a "better" solution of something.

Imagine a system where time in general is counted in seconds, and the system_clock is just that - the system_clock::period will return 1 second. This time is stored as a single 64-bit integer.

Now, in the same system, there is also a time in nano-seconds, but it's stored as a 128-bit integer. The resulting time-calculations are slightly more complex due to this large integer format, and for someone that only needs 1s precision for their time (in a system where a large number of calculations on time are made), you wouldn't want to have the extra penalty of using high_precision_clock, when the system doesn't need it.

As to if there are such things in real life, I'm not sure. The key is that it's not a violation to the standard, if you care to implement it such.

Note that steady is very much a property of "what happens when the system changes time" (e.g. if the outside network has been down for a several days, and the internal clock in the system has drifted off from the atomic clock that the network time updates to). Using steady_clock will guarantee that time doesn't go backwards or suddenly jumps forward 25 seconds all of a sudden. Likewise, there is no problem when there is a "leap second" or similar time adjustment in the computer system. On the other hand, a system_clock is guaranteed to give you the correct new time if you give it a forward duration past a daylight savings time, or some such, where steady_clock will just tick along hour after hour, regardless. So choosing the right one of those will affect the recording of your favourite program in the digital TV recorder - steady_clock will record at the wrong time [my DTV recorder did this wrong a few years back, but they appear to have fixed it now].

system_clock should also take into account the user (or sysadmin) changing the clock in the system, steady_clock should NOT do so.

Again, high_resolution_clock may or may not be steady - it's up to the implementor of the C++ library to give the appropriate response to is_steady.

In the 4.9.2 version of <chrono>, we find this using high_resolution_clock = system_clock;, so in this case it's a direct typedef (by a different name). But the spec doesn't REQUIRE this.

like image 64
Mats Petersson Avatar answered Nov 08 '22 22:11

Mats Petersson