mercredi 27 mai 2015

C++11 Atomic memory order with non-atomic variables

I am unsure about how the memory ordering guarantees of atomic variables in c++11 affect operations to other memory.

Let's say I have one thread which periodically calls the write function to update a value, and another thread which calls read to get the current value. Is it guaranteed that the effects of d = value; will not be seen before effects of a = version;, and will be seen before the effects of b = version;?

atomic<int> a {0};
atomic<int> b {0};
double d;

void write(int version, double value) {
    a = version;
    d = value;
    b = version;
}

double read() {
    int x,y;
    double ret;
    do {
        x = b;
        ret = d;
        y = a;
    } while (x != y);
    return ret;
}

Aucun commentaire:

Enregistrer un commentaire