Memory ordering
We have already learned about the atomic types and atomic operators available in the standard library. While performing operations on atomic types, we need to specify memory ordering for certain operations. Now, we will talk about the significance and use cases for the different memory-ordering semantics. The key idea behind atomic operations is to provide synchronization in data access across multiple threads, and this is achieved by enforcing the order of execution. For example, if writing to the data happens before the read from the data, things will be fine. Otherwise, you are in trouble! There are six memory-ordering options available with the standard library that can be applied to operations on atomic types: memory_order_relaxed
, memory_order_consume
, memory_order_acquire
, memory_order_release
, memory_order_acq_rel
, and memory_order_seq_cst
. For all atomic operations on atomic types, memory_order_seq_cst
is the memory order by default unless you specify something else...