Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Concurrent writes to different locations in the same cache line

Suppose I have a C++11 application where two threads write to different but nearby memory locations, using simple pointers to primitive types. Can I be sure that both these writes will end up in memory eventually (probably after both have reached a boost::barrier), or is there a risk that both CPU cores hold their own cache line containing that data, and the second core flushing its modification to RAM will overwrite and undo the modification done by the first write?

I hope that cache coherence will take care of this for me in all situations and on all setups compliant with the C++11 memory model, but I'd like to be sure.

like image 768
MvG Avatar asked Feb 01 '13 06:02

MvG


1 Answers

Yes the cache coherency mechanisms will take care of this. This is called False sharing and should be avoided by better separating the data to increase performance.

like image 99
hansmaad Avatar answered Oct 27 '22 00:10

hansmaad