c++ - Does C++11 guarantee memory ordering between a release fence and a consume operation? -
consider following code:
struct payload { std::atomic< int > value; }; std::atomic< payload* > pointer( nullptr ); void thread_a() { payload* p = new payload(); p->value.store( 10, std::memory_order_relaxed ); std::atomic_thread_fence( std::memory_order_release ); pointer.store( p, std::memory_order_relaxed ); } void thread_b() { payload* p = pointer.load( std::memory_order_consume ); if ( p ) { printf( "%d\n", p->value.load( std::memory_order_relaxed ) ); } }
does c++ make guarantees interaction of fence in thread consume operation in thread b?
i know in example case can replace fence + atomic store store-release , have work. question particular case using fence.
reading standard text can find clauses interaction of release fence acquire fence, , of release fence acquire operation, nothing interaction of release fence , consume operation.
replacing consume acquire make code standards-compliant, think. far understand memory ordering constraints implemented processors, should require weaker 'consume' ordering in thread b, memory barrier forces stores in thread visible before store pointer, , reading payload dependent on read pointer.
does standard agree?
your code works.
i know in example case can replace fence + atomic store store-release , have work. question particular case using fence.
fence relaxed atomic operation stronger corresponded atomic operation. e.g. (from http://en.cppreference.com/w/cpp/atomic/atomic_thread_fence, notes):
while atomic store-release operation prevents preceding writes moving past store-release, atomic_thread_fence memory_order_release ordering prevents preceding writes moving past subsequent stores.
Comments
Post a Comment