In this post, our tour through the c++ memory model goes one step deeper. Until now, the posts were only about the atomicity of the atomic data types but now we deal with the synchronisation and ordering constraints of the operations.
You can not configure the atomicity of an atomic data type, but you can adjust very accurately the synchronisation and ordering constraints of atomic operations. A leverage, which is unique to C++. That's not possible in the C#''s or Java's memory model.
The six variants of the C++ memory model
C++ has six variants of the memory model. The default for atomic operations is std::memory_order_seq_cst. But you can explicitly specify one of the other five. But what has C++11 to offer?
It helps a lot to answer two questions, to get a system into the six memory models.
- For which type of atomic operations should you use the memory model?
- Which synchronisation and ordering constraints are defined by the memory model?
The rest of this post is about answering these questions. So what are the types of atomic operations?
Types of atomic operations
The memory model deals with reading and/or writing atomic operations.
- read operation: memory_order_acquire and memory_order_consume
- write operation: memory_order_release
- read-modify-write operation: memory_order_acq_rel and memory_order_seq_cst
memory_order_relaxed defines no synchronisation and ordering constraints. So it will not fit in this taxonomy.
The table orders the atomic operations based on their reading and/or writing characteristics.
In case you use an atomic operation atomVar.load(5) with a memory model, that is designed for a write or read-modify-write operation, the write part has no effect. So an atomVar.load(5,std, std::memory_order_acq_rel) is equivalent to an atomVar.load(5,std::memory_order_acquire), an atomVar.load(5, std::memory_order_release) is equivalent to an atomVar.load(5, std::memory_order_relaxed).
The different synchronisation and ordering constraints
There are three different types synchronization and ordering constraints in C++11:
- Sequential consistency: memory_order_seq_cst
- Acquire-release: memory_order_consume, memory_order_acquire, memory_order_release and memory_order_acq_rel
- Relaxed: memory_order_relaxed
While the sequential consistency establishes a global order between threads, the acquire-release semantic establishes an ordering between read and write operations on the same atomic variable on different threads. The relaxed semantic only guarantees that operations on the same atomic data type in the same thread can not be reordered. That guarantee is called modification order consistency. But other threads can see this operation in a different order.
Admit, that was a short post. But I will stick to my idea, to talk only on one topic in one post.
Especially the levels of the different memory models, their effects on atomic and non-atomic operations, makes the C++ memory model to a thrilling but also challenging topic. In the next post I will talk about the synchronization and ordering constraints of the sequential consistency, the acquire-release semantic, and the relaxed semantic. I will do it in theory and practice. The next post with be about the application of the sequential consistency. The base is laid out in the post sequential consistency.
Thanks a lot to my Patreon Supporters: Matt Braun, Roman Postanciuc, Tobias Zindl, Marko, G Prvulovic, Reinhold Dröge, Abernitzke, Frank Grimm, Sakib, Broeserl, António Pina, Darshan Mody, Sergey Agafyin, Андрей Бурмистров, Jake, GS, Lawton Shoemake, Animus24, Jozo Leko, John Breland, espkk, Wolfgang Gärtner, Louis St-Amour, Stephan Roslen, Venkat Nandam, Jose Francisco, Douglas Tinkham, Kuchlong Kuchlong, Avi Kohn, Robert Blanch, Truels Wissneth, Kris Kafka, Mario Luoni, Neil Wang, Friedrich Huber, Sudhakar Balagurusamy, lennonli, and Pramod Tikare Muralidhara.
Thanks in particular to Jon Hess, Lakshman, Christian Wittenhorst, Sherhy Pyton, and Dendi Suhubdy
I'm happy to give online-seminars or face-to-face seminars world-wide. Please call me if you have any questions.
Here is a compilation of my standard seminars. These seminars are only meant to give you a first orientation.