steady_clock class and clock() function are non conformant with C++11 and C11 standards - by PowerGamer1

Status : 

  Fixed<br /><br />
		This item has been fixed in the current or upcoming version of this product.<br /><br />
		A more detailed explanation for the resolution of this particular item may have been provided in the comments section.

Sign in
to vote
ID 753115 Comments
Status Closed Workarounds
Type Bug Repros 2
Opened 7/10/2012 9:09:34 AM
Access Restriction Public


A quote from C11 standard:
The clock function returns the implementation’s best approximation to the processor
time used by the program since the beginning of an implementation-defined era related
only to the program invocation.

A few quotes from the C++11 standard:
20.11.3 Clock requirements
In Table 59 C1 and C2 denote clock types. t1 and t2 are values returned by C1::now() where the call returning t1 happens before (1.10) the call returning t2 and both of these calls occur before C1::time_point::max(). [ Note: this means C1 did not wrap around between t1 and t2. -end note ]

Table 59 — Clock requirements
C1::is_steady const bool true if t1 <= t2 is always true and the time between clock ticks is constant, otherwise false. Class steady_clock
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.

The whole point of steady_clock (and difference with system_clock) - the steady_clock "goes on forward" even if the hardware clock on the PC is rewinded back (by user, by program, etc.).

The same idea is behind the return value of C language clock() function.

As you can see - these requirements of C++11 (and C11) standard are not carried out by VC++ 2012 RC implementation (as the program below shows), which is not surprising at all, seeing that steady_clock::now() and clock() are implemented using GetSystemTimeAsFileTime() Win32 API function. GetTickCount/GetTickCount64/QueryPerformanceCounter functions should be used for that instead.
Sign in to post a comment.
Posted by Microsoft on 3/17/2014 at 4:52 PM

Thanks for reporting this bug. We've fixed it, and the fix will be available in the next major version of VC (i.e. after 2013).

steady_clock and high_resolution_clock are now synonyms powered by QueryPerformanceCounter() converted to nanoseconds. QPC meets the Standard's requirements for steadiness/monotonicity.

Additionally, the CRT's clock() has been reimplemented with QPC. While this improves precision and conformance to the C Standard (as QPC is monotonic), we are aware that this is not completely conformant. Our CRT maintainer has chosen to avoid having clock() return CPU time advancing faster than 1 second per physical second, which could silently break programs depending on the previous behavior.

Stephan T. Lavavej
Senior Developer - Visual C++ Libraries
Posted by Microsoft on 2/21/2013 at 1:48 PM

Thanks for reporting this bug. I wanted to let you know what's happening with it. I'm still keeping track of it, but it's been resolved as "Deferred" because we may not have time to fix it in VC12. (Note: VC8 = VS 2005, VC9 = VS 2008, VC10 = VS 2010, VC11 = VS 2012.)

Note: Connect doesn't notify me about comments. If you have any further questions, please E-mail me.

Stephan T. Lavavej
Senior Developer - Visual C++ Libraries
Posted by Microsoft on 7/10/2012 at 11:14 PM
Thanks for your feedback.

We are rerouting this issue to the appropriate group within the Visual Studio Product Team for triage and resolution. These specialized experts will follow-up with your issue.
Posted by Microsoft on 7/10/2012 at 9:49 AM
Thank you for your feedback, we are currently reviewing the issue you have submitted. If this issue is urgent, please contact support directly(