c++ - Acquire/Release versus Sequentially Consistent memory order -
for std::atomic<t>
t primitive type:
if utilize std::memory_order_acq_rel
fetch_xxx
operations, , std::memory_order_acquire
load
operation , std::memory_order_release
store
operation blindly (i mean resetting default memory ordering of functions)
std::memory_order_seq_cst
(which beingness used default) of declared operations? if results same, usage anyhow different using std::memory_order_seq_cst
in terms of efficiency?
the c++11 memory ordering parameters atomic operations specify constraints on ordering. if store std::memory_order_release
, , load thread reads value std::memory_order_acquire
subsequent read operations sec thread see values stored memory location first thread prior store-release, or later store of memory locations.
if both store , subsequent load std::memory_order_seq_cst
relationship between these 2 threads same. need more threads see difference.
e.g. std::atomic<int>
variables x
, y
, both 0.
thread 1:
x.store(1,std::memory_order_release);
thread 2:
y.store(1,std::memory_order_release);
thread 3:
int a=x.load(std::memory_order_acquire); // x before y int b=y.load(std::memory_order_acquire);
thread 4:
int c=y.load(std::memory_order_acquire); // y before x int d=x.load(std::memory_order_acquire);
as written, there no relationship between stores x
, y
, quite possible see a==1
, b==0
in thread 3, , c==1
, d==0
in thread 4.
if memory orderings changed std::memory_order_seq_cst
enforces ordering between stores x
, y
. consequently, if thread 3 sees a==1
, b==0
means store x
must before store y
, if thread 4 sees c==1
, meaning store y
has completed, store x
must have completed, must have d==1
.
in practice, using std::memory_order_seq_cst
everywhere add together additional overhead either loads or stores or both, depending on compiler , processor architecture. e.g. mutual technique x86 processors utilize xchg
instructions rather mov
instructions std::memory_order_seq_cst
stores, in order provide necessary ordering guarantees, whereas std::memory_order_release
plain mov
suffice. on systems more relaxed memory architectures overhead may greater, since plain loads , stores have fewer guarantees.
memory ordering hard. devoted entire chapter in my book.
c++ concurrency c++11 atomic
No comments:
Post a Comment