Is steady_clock monotonic across threads?

前端 未结 1 511
长情又很酷
长情又很酷 2021-01-18 05:34

Are monotonic properties of std::chrono::steady_clock preserved across threads? For example, suppose I have the following program.

#include <         


        
1条回答
  •  一整个雨季
    2021-01-18 05:39

    Standard [time.clock.steady]

    ...
    static constexpr bool is_steady = true;
    static time_point now() noexcept;
    ...  
    

    is_steady has to be true in all implementations (ie. the class can not exist with false, if the OS etc. isn't capable of it), and both members are independent of instances.

    Standard [time.clock.req]:

    Clock requirements
    ...
    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().
    ...
    C1::is_steady: true if t1 <= t2 is always true and the time between clock ticks is constant, otherwise false.

    And the 1.10 part contains:

    Multi-threaded executions and data races
    ...
    An evaluation A happens before an evaluation B if:
    A is sequenced before B, or
    A inter-thread happens before B.
    ...
    An evaluation A inter-thread happens before an evaluation B if
    A synchronizes with B, or ...

    I don't think synchronizing needs to be copied here (a mutex should be enough to fulfil that),
    so: Yes, it's ok.

    0 讨论(0)
提交回复
热议问题