False Sharing
When a processor reads a variable such as an int
from main memory, it reads more than the size of an int
from memory. The processor reads an entire cache line (typically 64 bytes) from memory. False sharing occurs if two threads read at the same time different variables a
and b
that are located on the same cache line. Although a
and b
are logically separated, they are physically connected. An expensive hardware synchronisation on the cache line is necessary because a
and b
share the same one. The result is that you get the right results, but the performance of your concurrent application decreases. Precisely this phenomenon happen in the following program.