Does standard C++11 guarantee that memory_order_seq_cst prevents StoreLoad reordering around an atomic operation for non-atomic memory accesses?
As known, there are 6 std::memory_orders in C++11, and its specifies how regular, non-atomic memory accesses are to be ordered around an atomic operation - Working Draft, Standard for Programming Language C++ 2016-07-12: http://ift.tt/2b1B2Om
§ 29.3 Order and consistency
§ 29.3 / 1
The enumeration memory_order specifies the detailed regular (non-atomic) memory synchronization order as defined in 1.10 and may provide for operation ordering. Its enumerated values and their meanings are as follows:
Also known, that these 6 memory_orders prevent some of these reordering:
But, does memory_order_seq_cst prevent StoreLoad reordering around an atomic operation for regular, non-atomic memory accesses or only for other atomic with the same memory_order_seq_cst?
I.e. to prevent this StoreLoad-reordering should we use std::memory_order_seq_cst for both STORE and LOAD, or only for one of it?
std::atomic<int> a, b;
b.store(1, std::memory_order_seq_cst); // Sequential Consistency
a.load(std::memory_order_seq_cst); // Sequential Consistency
About Acquire-Release semantic is all clear, it specifies exactly non-atomic memory-access reordering across atomic operations: http://ift.tt/1krieqi
To prevent StoreLoad-reordering we should use std::memory_order_seq_cst.
Two examples:
std::memory_order_seq_cstfor both STORE and LOAD: there isMFENCE
StoreLoad can't be reordered - GCC 6.1.0 x86_64: http://ift.tt/2bBxPmd
std::atomic<int> a, b;
b.store(1, std::memory_order_seq_cst); // can't be executed after LOAD
a.load(std::memory_order_seq_cst); // can't be executed before STORE
std::memory_order_seq_cstfor LOAD only: there isn'tMFENCE
StoreLoad can be reordered - GCC 6.1.0 x86_64: http://ift.tt/2bl1eDf
std::atomic<int> a, b;
b.store(1, std::memory_order_release); // can be executed after LOAD
a.load(std::memory_order_seq_cst); // can be executed before STORE
Also if C/C++-compiler used alternative mapping of C/C++11 to x86, which flushes the Store Buffer before the LOAD: MFENCE,MOV (from memory), so we must use std::memory_order_seq_cst for LOAD too: http://ift.tt/10u9Oq9 As this example is discussed in another question as approach (3): Does it make any sense instruction LFENCE in processors x86/x86_64?
I.e. we should use std::memory_order_seq_cst for both STORE and LOAD to generate MFENCE guaranteed, that prevents StoreLoad reordering.
Is it true, that memory_order_seq_cst for atomic Load or Store:
-
specifi Acquire-Release semantic - prevent: LoadLoad, LoadStore, StoreStore reordering around an atomic operation for regular, non-atomic memory accesses,
-
but prevent StoreLoad reordering around an atomic operation only for other atomic operations with the same
memory_order_seq_cst?
Aucun commentaire:
Enregistrer un commentaire