jeudi 19 décembre 2019

Is memory fence needed after joining another thread without any lock?

I am wondering if I need to put a memory fence after thread join if I want to read values stored from that thread immediately. Does thread join already imply a memory fence?

vector<int> v(1 << 21);

thread th([&]() {
    for (int i = 0; i < (1 << 20); i++) {
        v[i] = i * i; // store some kind of calculation results into the vector
    }
});

for (int i = (1 << 20); i < (1 << 21); i++) {
    v[i] = i * i;
}

th.join();

// Is any memory fence needed to be here?

// use the values from another thread... 
printf("%d\n", v[1234]);
// ...

Aucun commentaire:

Enregistrer un commentaire