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 regularint
(or any type for that matter).The C++ standard supports your case with the following phrase (§ 1.10.1-6):
Since
threadB
loads the value ofready
stored bythreadA
(it is waiting for it in a loop), the synchronizes-with relationship is established. Therefore,a.load()
observes the memory effects ofa.store()
. Another way to say this is thata.store()
happens-beforea.load()