mardi 15 août 2017

mix atomic and non atomic variables and caches

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?

Aucun commentaire:

Enregistrer un commentaire