...
/CppMem: Atomics with an Acquire-Release Semantic
CppMem: Atomics with an Acquire-Release Semantic
This lesson gives an overview of atomics with acquire-release semantic used in the context of CppMem.
We'll cover the following...
The synchronization in the acquire-release semantic takes place between atomic operations on the same atomic. This is in contrast to the sequential consistency where we have synchronization between threads. Due to this fact, the acquire-release semantic is more lightweight and, therefore, faster.
Here is the program with acquire-release semantic:
Press + to interact
// ongoingOptimisationAcquireRelease.cpp#include <atomic>#include <iostream>#include <thread>std::atomic<int> x{0};std::atomic<int> y{0};void writing(){x.store(2000, std::memory_order_relaxed);y.store(11, std::memory_order_release);}void reading(){std::cout << y.load(std::memory_order_acquire) << " ";std::cout << x.load(std::memory_order_relaxed) << std::endl;}int main(){std::thread thread1(writing);std::thread thread2(reading);thread1.join();thread2.join();};
On first glance you will notice that all operations are atomic, so the program is well-defined. But the second glance shows ...
Access this course and 1400+ top-rated courses and projects.