The Two Flavors of Transactional Memory
Explore the two types of transactional memory in C++20: synchronized and atomic blocks. Understand how synchronized blocks ensure total order but may include transaction-unsafe code, while atomic blocks enforce transaction safety with different exception management options. Learn about transaction-safe versus unsafe functions and how these concepts impact concurrency and data races.
We'll cover the following...
C++ supports transactional memory in two flavors: synchronized blocks and atomic blocks.
Synchronized & Atomic Blocks
Up to now, I only wrote about transactions. Now, I will write about synchronized blocks and atomic blocks. Both can be encapsulated in each other; specifically, synchronized blocks are not atomic blocks because they can execute transaction-unsafe. An example would be code like the output to the console which can not be undone. For this reason, synchronized blocks are often called relaxed blocks.
Synchronized Blocks
Synchronized blocks behave like they are protected by a global lock, i.e. This means that all synchronized blocks follow a total order. In particular: all changes to a synchronized block are available in ...