c++ - mix atomic and non atomic variables and caches -
let's have piece of code correct (i hope @ 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 : in case using int a;
instead of std::atomic<int> a;
, correct well? or there problem of cache flushing / invalidation?
whether or not idea, example, code fine..
you may replace atomic type of a
regular int
(or type matter).
c++ standard supports case following phrase (§ 1.10.1-6):
certain library calls synchronize other library calls performed thread. example, atomic store-release synchronizes load-acquire takes value store
since threadb
loads value of ready
stored threada
(it waiting in loop), synchronizes-with relationship established. therefore, a.load()
observes memory effects of a.store()
. way a.store()
happens-before a.load()
Comments
Post a Comment