My previous post was related to discussion about memory model in C++11. Now let’s talk about memory model itself briefly.
So, memory model contains 3 different use cases. It can be classified differently. Let’s use the following criteria: how many threads are affected on atomic operation:
- One thread, known as relaxed atomic operations.
- Two threads, known as acquire-release operations.
- More than two threads, the strongest guarantee, known as sequentially consistent operations.
One Thread: Relaxed Atomics
It’s quite simple: if you just need to have eventual atomic consistency you may use relaxed atomics. It has the best performance but it guarantees only atomic operation for that value. Other threads may read old values, but eventually updated value will appear. It’s useful, e.g., for statistics, debugging flags etc, or during intermediate atomic operations in some complex scenarios.