Synchronization and Ordering Constraints

Contents[Show]

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 synchronization 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. Leverage, which is unique to C++. That's impossible 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?
 

enum memory_order{
  memory_order_relaxed,
  memory_order_consume,
  memory_order_acquire,
  memory_order_release,
  memory_order_acq_rel,
  memory_order_seq_cst
}

 

It helps to answer two questions to get a system into the six memory models.

  1. For which type of atomic operations should you use the memory model?
  2. Which synchronization 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?

 

Rainer D 6 P2 540x540Modernes C++ Mentoring

Be part of my mentoring programs:

 

 

 

 

Do you want to stay informed about my mentoring programs: Subscribe via E-Mail.

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_cs

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.

atomicOperationsReadWriteEng

If you use an atomic operation atomVar.load(5) with a memory model designed for a write or read-modify-write operation, the writing 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 synchronization and ordering constraints

There are three different types of 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 reading 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.

What's next?

Admit, that was a short post. But I will stick to my idea of discussing only one topic in one post.

Especially the levels of the different memory models and their effects on atomic and non-atomic operations make the C++ memory model a thrilling but also challenging topic. In the next post, I will discuss sequential consistency's synchronization and ordering constraints, the acquire-release semantic, and the relaxed semantic. I will do it in theory and practice. The following post with be about the application of 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, G Prvulovic, Reinhold Dröge, Abernitzke, Frank Grimm, Sakib, Broeserl, António Pina, Sergey Agafyin, Андрей Бурмистров, Jake, GS, Lawton Shoemake, Animus24, Jozo Leko, John Breland, Venkat Nandam, Jose Francisco, Douglas Tinkham, Kuchlong Kuchlong, Robert Blanch, Truels Wissneth, Kris Kafka, Mario Luoni, Friedrich Huber, lennonli, Pramod Tikare Muralidhara, Peter Ware, Daniel Hufschläger, Alessandro Pezzato, Bob Perry, Satish Vangipuram, Andi Ireland, Richard Ohnemus, Michael Dunsky, Leo Goodstadt, John Wiederhirn, Yacob Cohen-Arazi, Florian Tischler, Robin Furness, Michael Young, Holger Detering, Bernd Mühlhaus, Matthieu Bolt, Stephen Kelley, Kyle Dean, Tusar Palauri, Dmitry Farberov, Juan Dent, George Liao, Daniel Ceperley, Jon T Hess, Stephen Totten, Wolfgang Fütterer, Matthias Grün, Phillip Diekmann, Ben Atakora, Ann Shatoff, and Rob North.

 

Thanks, in particular, to Jon Hess, Lakshman, Christian Wittenhorst, Sherhy Pyton, Dendi Suhubdy, Sudhakar Belagurusamy, Richard Sargeant, Rusty Fleming, John Nebel, Mipko, Alicja Kaminska, and Slavko Radman.

 

 

My special thanks to Embarcadero CBUIDER STUDIO FINAL ICONS 1024 Small

 

My special thanks to PVS-Studio PVC Logo

 

My special thanks to Tipi.build tipi.build logo

 

My special thanks to Take Up Code TakeUpCode 450 60

 

Seminars

I'm happy to give online seminars or face-to-face seminars worldwide. Please call me if you have any questions.

Bookable (Online)

German

Standard Seminars (English/German)

Here is a compilation of my standard seminars. These seminars are only meant to give you a first orientation.

  • C++ - The Core Language
  • C++ - The Standard Library
  • C++ - Compact
  • C++11 and C++14
  • Concurrency with Modern C++
  • Design Pattern and Architectural Pattern with C++
  • Embedded Programming with Modern C++
  • Generic Programming (Templates) with C++

New

  • Clean Code with Modern C++
  • C++20

Contact Me

Modernes C++,

RainerGrimmDunkelBlauSmall

Comments   

-1 #1 Jade 2016-07-19 21:58
Good day very nice site!! Man .. Beautiful .. Amazing ..

I'll bookmark your website and take the feeds also?
I am glad to seek out numerous helpful info here
within the put up, we want develop more techniques on this
regard, thanks for sharing. . . . . .
Quote
-1 #2 kontentmachine.com 2016-07-19 23:17
Hi, of course this paragraph is in fact good and I have learned
lot of things from it about blogging. thanks.
Quote
-1 #3 Vanessa 2016-07-20 08:58
I've been surfing on-line more than three hours lately, yet I by no
means found any fascinating article like yours. It is lovely worth sufficient for me.
In my view, if all webmasters and bloggers made good content material as you
did, the web shall be a lot more useful than ever
before.
Quote
-1 #4 Paula 2016-07-24 14:53
Thanks for another informative website. Where else may just I get
that kind of info written in such a perfect approach? I've a undertaking that I'm
just now operating on, and I've been at the look out for such
information.
Quote
-1 #5 WilmaWMcgann 2016-11-20 02:21
Many thanks for finally writing about >Synchronization and ordering
constraints
Quote
-1 #6 Florrie 2019-04-03 04:02
Hi there! Such a great short article, thank you!
Quote

Stay Informed about my Mentoring

 

Mentoring

English Books

Course: Modern C++ Concurrency in Practice

Course: C++ Standard Library including C++14 & C++17

Course: Embedded Programming with Modern C++

Course: Generic Programming (Templates)

Course: C++ Fundamentals for Professionals

Course: The All-in-One Guide to C++20

Course: Master Software Design Patterns and Architecture in C++

Subscribe to the newsletter (+ pdf bundle)

All tags

Blog archive

Source Code

Visitors

Today 1543

Yesterday 6503

Week 27800

Month 8046

All 12086255

Currently are 256 guests and no members online

Kubik-Rubik Joomla! Extensions

Latest comments