IT lexicon › Programming › False sharing

False sharing

Programming På svenska → Updated: 2026-07-29

Two threads write to different variables that happen to sit in the same cache line. The coherence protocol invalidates the whole line on every write and the threads fight over something they don't actually share.

The symptom is a parallelized loop that gets slower the more cores you give it. Classic case: an array with one counter per thread, long counters[8] — all eight fit in a single 64-byte line. The fix is padding or alignment so each hot field gets its own line: alignas(64) in C++, @Contended in Java (needs -XX:-RestrictContended), #[repr(align(64))] in Rust. Find it with perf c2c — never guess.

← Back to the lexicon