Let's say we have this piece of code that it is correct (I hope at least) :
std::atomic<int> a;
std::atomic<bool> ready{false};
void threadA() {
a.store(666, std::memory_order_relaxed);
ready.store(true, std::memory_order_release);
}
void threadB() {
while(!ready.load(std::memory_order_acquire));
process(a.load(std::memory_order_relaxed));
}
My question is : In the case you are using a int a;
instead of std::atomic<int> a;
, it is correct as well? Or is there a problem of cache flushing / invalidation?
Whether or not this is a good idea, as an example, your code is fine..
You may replace the atomic type of a
with a regular int
(or any type for that matter).
The C++ standard supports your case with the following phrase (§ 1.10.1-6):
Certain library calls synchronize with other library calls performed by another thread. For example, an atomic store-release synchronizes with a load-acquire that takes its value from the store
Since threadB
loads the value of ready
stored by threadA
(it is waiting for it in a loop), the synchronizes-with relationship is established.
Therefore, a.load()
observes the memory effects of a.store()
. Another way to say this is that a.store()
happens-before a.load()