Acquire Release: The Typical Misunderstanding
Understand the typical pitfalls in using acquire-release semantics in C++ concurrency. Learn how synchronization and ordering constraints work to prevent data races, focusing on happens-before and synchronizes-with relations through practical examples.
We'll cover the following...
What is my motivation for writing about the typical misunderstanding of the acquire-release semantic? Many of my readers and students have already fallen into this trap. Let’s look at the straightforward case. Here is a simple program as a starting point.
The consumer thread t1 in line 17 waits until the consumer thread t2 in line 13 sets dataProduced to true. dataProduced is the guard and it guarantees that access to the non-atomic variable mySharedWork is synchronized. This means that the producer thread t2 initializes mySharedWork, then the consumer thread t1 finishes the work by setting mySharedWork[1] ...