mercredi 29 mars 2017

Why there is no data race?

I am reading Bjarne's FAQ on Memory Model, here is a quote

So, C++11 guarantees that no such problems occur for "separate memory locations.'' More precisely: A memory location cannot be safely accessed by two threads without some form of locking unless they are both read accesses. Note that different bitfields within a single word are not separate memory locations, so don't share structs with bitfields among threads without some form of locking. Apart from that caveat, the C++ memory model is simply "as everyone would expect.''

However, it is not always easy to think straight about low-level concurrency issues. Consider:

start with x==0 and y==0

if (x) y = 1; // Thread 1

if (y) x = 1; // Thread 2

Is there a problem here? More precisely, is there a data race? (No there isn't).

My question is, why there is no data race? It is obvious to me that there apparently is a data race since thread 1 is a writer for y while thread 2 is a reader for y, and similarly for x.

Aucun commentaire:

Enregistrer un commentaire