Relaxed Semantic

This lesson gives an overview of relaxed semantic which is used in C++ for concurrency.

The relaxed semantic is the other end of the spectrum. It’s the weakest of all memory models and only guarantees that the operations on the same atomic data type in the same thread won’t be reordered. That guarantee is called modification order consistency. Other threads can see these operations in a different order.

No Synchronization & Ordering constraints?

This is quite easy; if there are no rules, we cannot violate them. But that is too easy, as the program should have well-defined behavior. In particular, this means that data races are not allowed. To guarantee this you typically use synchronization and ordering constraints of stronger memory models to control operations with relaxed semantic. How does this work? A thread can see the effects of another thread in arbitrary order, so you have to make sure there are points in your program where all operations on all threads get synchronized.

A typical example of an atomic operation, in which the sequence of operations doesn’t matter, is a counter. The key observation for a counter is not in which order the different threads increment the counter; it’s that all increments are atomic and all threads’ tasks are done at the end. Have a look at the following example.

Get hands-on with 1200+ tech skills courses.