CppMem: Atomics with Non-Atomics
This lesson highlights atomics with non-atomics used in the context of CppMem.
We'll cover the following...
A typical misunderstanding in the application of the acquire-release semantic is to assume that the acquire operation is waiting for the release operation. Based on this wrong assumption, you may think that x
does not have to be an atomic variable and we can further optimize the program.
Press + to interact
// ongoingOptimisationAcquireReleaseBroken.cpp#include <atomic>#include <iostream>#include <thread>int x = 0;std::atomic<int> y{0};void writing(){x = 2000;y.store(11, std::memory_order_release);}void reading(){std::cout << y.load(std::memory_order_acquire) << " ";std::cout << x << std::endl;}int main(){std::thread thread1(writing);std::thread thread2(reading);thread1.join();thread2.join();};
The program has a data ...