Search

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

Closed
as Deferred Help for as Deferred

6
0
Sign in
to vote
Type: Bug
ID: 753115
Opened: 7/10/2012 9:09:34 AM
Access Restriction: Public
0
Workaround(s)
0
User(s) can reproduce this bug
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.

20.11.7.2 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.
Details (expand)

Visual Studio/Team Foundation Server/.NET Framework Tooling Version

Visual Studio 2012 RC

Steps to reproduce

Run the following program with admin rights (it rewinds system clock 1 hour back; after running this program set the system clock back to correct time manually).

int main()
{
    auto t1 = std::chrono::steady_clock::now();
    clock_t x1 = std::clock();
    SYSTEMTIME st;
    GetSystemTime(&st);
    st.wHour--; // for simplicity assume wHour != 0 before decrement
    SetSystemTime(&st); // requires admin rights
    // assuming the call to SetSystemTime() was successful
    auto t2 = std::chrono::steady_clock::now();
    clock_t x2 = std::clock();
    std::cout << std::boolalpha << (t1 <= t2) << std::endl;
    std::cout << std::boolalpha << (x1 <= x2) << std::endl;
}

Product Language

English

Operating System

Windows 7

Operating System Language

English

Actual results

false
false

Expected results

true
true
File Attachments
0 attachments
Sign in to post a comment.
Posted by Microsoft on 2/21/2013 at 1:48 PM
Hi,

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
stl@microsoft.com
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(http://support.microsoft.com)
Sign in to post a workaround.