c++11 std::atomic and memory_order_relaxed
Posted: Tue Apr 01, 2014 9:54 am
I thought i had an idea how this worked in theory, but having just recently done some reading one of the examples in the specification has me quite confused:
from http://www.open-std.org/jtc1/sc22/wg21/ ... mics.order
from http://www.open-std.org/jtc1/sc22/wg21/ ... mics.order
My understanding of memory_order_relaxed was that the atomic operations can be rearranged in any order but pulling numbers out of thin air would be forbidden. what rearrangement of loads and stores leads to r1 == r2 == 42 in this situation?[Note: The requirements do allow r1 == r2 == 42 in (x, y initially zero):
Thread 1:
r1 = x.load( memory_order_relaxed );
if ( r1 == 42 ) y.store( r1, memory_order_relaxed );
Thread 2:
r2 = y.load( memory_order_relaxed );
if ( r2 == 42 ) x.store( 42, memory_order_relaxed );
Implementations are discouraged from allowing such behavior. —end note]