I want to ensure the following three statements execute in the exact order specified:
auto t1 = std::chrono::steady_clock::now(); // Statement 1
auto t2 = std::chrono::system_clock::now(); // Statement 2
auto t3 = std::chrono::steady_clock::now(); // Statement 3
The compiler (or processor) is free to reorder these statements as there is no data dependency. See https://stackoverflow.com/a/38025837/1520427
C++11 added std::atomic_signal_fence
to "establish memory synchronization ordering of non-atomic and relaxed atomic accesses, as instructed by order, between a thread and a signal handler executed on the same thread." However, according to cppreference, "no CPU instructions for memory ordering are issued" so I'm unclear how this would stop the processor from reordering things.
My questions:
Is the following code sufficient to stop the compiler from reordering statements (assuming it has local definitions for all of the code)?
auto t1 = std::chrono::steady_clock::now(); // Statement 1
std::atomic_signal_fence(std::memory_order::memory_order_release);
auto t2 = std::chrono::system_clock::now(); // Statement 2
std::atomic_signal_fence(std::memory_order::memory_order_release);
auto t3 = std::chrono::steady_clock::now(); // Statement 3
std::atomic_signal_fence(std::memory_order::memory_order_release);
Is the processor free to rearrange the order of these operations? e.g. can it execute them 2-1-3? If so, would std::atomic_thread_fence
prevent it?
Do I need to introduce an artificial data dependency (as in the linked question) to get the intended behaviour?
Aucun commentaire:
Enregistrer un commentaire