I was reading about MESI protocol and cannot understand why there is data race if we have exclusive access on every write operation which consequently invalidated cache lines in other cores' caches? in this example:
CYCLE # CORE 1 CORE 2
0 reg = load(&counter);
1 reg = reg + 1; reg = load(&counter);
2 store(&counter, reg); reg = reg + 1;
3 store(&counter, reg);
it's said that the overall result is that the variable is only incremented once while both cores try to increment it (result is expected to be two). So the question is if during writing operation both cores request exclusive access to the cache line (and thus other cores "wait" their turn to modify and thus get also exclusive access) why there is data race on that variable?