False Sharing
CPUs move memory in fixed blocks called cache lines, often 64 bytes. A core caches a whole line, not a single byte. False sharing happens when two threads modify different variables that happen to sit on the same cache line.
Even though the variables are logically independent, the hardware cache coherence protocol treats the line as one unit. When core one writes its variable, it invalidates the line in core two's cache, forcing core two to reload. The two cores end up ping ponging the line back and forth, destroying performance with no actual data sharing.
Symptoms include code that scales worse as you add threads, even though each thread touches its own data.
Fixes spread the variables apart so they land on different lines:
- Padding add filler bytes so each hot variable owns its own cache line.
- Alignment use cache line aligned structures.
- Per thread storage give each thread a separate object instead of an array of adjacent counters.
False sharing is invisible in source code, so profiling cache misses is often how it is found.
Key idea
False sharing occurs when independent variables share a cache line; padding or alignment separates them and restores scaling.