Cache lines
The data in a cache is grouped into blocks
Called cache-lines, which are typically 64 or 128 bytes wide. These are the smallest units of memory that
Can be read from, or written to, main memory. this works well in most programs as data that is close in memory is often needed close in time by a participant thread. however, this is the root of the false sharing problem.
Cache Coherence
When a program writes a value to memory, it goes firstly to the cache
The core that ran the Code. If any other caches hold a copy of that cache line,
Their copy is marked as invalid and cannot be used. The new value is written to main memory, and the other caches
Must re-read it from there if they need it. although this synchronization is implemented in hardware, it still takes time. and, of course, reading from Main Memory takes a few hundred clock cycles by itself.
Modern processors use the MESI Protocol to implement cache coherence.
This basically means eachcache line can be in one of four states:
- MOdified
- EXclusive
- SHared
- INvalid
When a core modifies any data in a cache line,
It transitions to "modified", and any other caches that hold a copy of the same cache line
Are forced to "invalid ". the other cores must then read the data from main memory next time they need it. that's all I need to say about this here. A detailed explanation is available on Wikipedia [^],
If you're interested.
False sharing
Imagine two different variables are being used by two different threads on two different cores. This appears to be embarrassingly parallel, as the different threads are using isolated data. However,
If the two variables are located in the same cache line and at least one is being written, then there will
Be contention for that cache line. This is false sharing.
It is called false sharing because
Even though the different threads are not sharing data, they are, unintentionally, sharing a cache line.
Source: http://www.codeproject.com/Articles/51553/Concurrency-Hazards-False-Sharing