Is stable_clock monotonous in streams?

Are the monotonous properties of std::chrono::steady_clock in streams? For example, suppose I have the following program.

 #include <chrono> #include <mutex> #include <thread> using namespace std; using namespace chrono; mutex m; int i = 0; void do_something(int &x) { x += 1; } void f1() { unique_lock<mutex> lock(m); auto time = steady_clock::now(); do_something(i); } void f2() { unique_lock<mutex> lock(m); auto time = steady_clock::now(); do_something(i); } int main() { thread t1(f1); thread t2(f2); t1.join(); t2.join(); return 0; } 

Can I assume that a thread with a lower time value at the end (suppose they have a completely different value) changed i in front of the other, and the other saw i , since it was the first?

+6
source share
1 answer

Standard [time.clock.steady]

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

is_steady must be true in all implementations (i.e. the class cannot exist with false if the OS, etc. is not capable of it), and both members are instance-independent.

Standard [time.clock.req]:

Watch requirements
...
C1 and C2 indicate the types of hours. t1 and t2 are the values ​​returned by C1 :: now (), where the call to t1 returns before (1.10) , the call that returns t2, and both calls occur before C1 :: time_-point :: max ().
...
C1 :: is_steady: true if t1 <= t2 is always true and the time between clock cycles is constant, otherwise false.

And part 1.10 contains:

Multithreaded Executions and Data Schedules
...
Grade A occurs before grade B if:
A is sequenced to B, or
Between threads occurs up to B.
...
Rating. Cross-threading occurs before score B if A is in sync with B or ...

I don’t think that synchronization needs to be copied here (for this mutex to be sufficient)
therefore: Yes , this is normal.

+7
source

Source: https://habr.com/ru/post/1012835/


All Articles