What is False sharing?
A slowdown in which threads update different variables that happen to share a cache line, so the line bounces between cores.
Japanese name: 偽共有
A slowdown in which threads update different variables that happen to share a cache line, so the line bounces between cores.
Japanese name: 偽共有
Cache coherence protocols such as MESI (Modified, Exclusive, Shared, Invalid) track ownership of whole cache lines, typically 64 bytes, rather than individual variables. Before a core can write, it must hold the line exclusively, which invalidates every other core's copy. If core 0 keeps writing variable a and core 1 keeps writing variable b in the same line, each write forces the line to move between the two caches. Neither thread reads the other's data, so the results stay correct, but an access that would be a few-cycle L1 hit becomes a slow cross-core transfer.
True sharing means threads really do access the same variable, such as a global counter or a lock word; the contention is inherent and can only be removed by changing the algorithm, for example by splitting the counter. False sharing means the data are logically independent and merely happen to be neighbors in memory, so the cure is a change of data layout. Both produce the same symptom: heavy coherence traffic on a few lines. Read-only sharing causes neither problem, because many caches can hold a line in the Shared state at once; only writes force invalidations.
The classic case is an array of per-thread counters: with 8-byte elements, eight of them fit in one 64-byte line, so up to eight threads fight over it. Other common sources are structs whose fields are updated by different threads, a lock placed in the same line as data guarded by a different lock, and work queues whose head and tail indices, written by producer and consumer, sit side by side. The usual sign is a program whose speedup flattens or even falls as threads are added although each thread appears to touch only its own data.
Pad or align each independently written item to the cache-line size, for example with alignas(64) in C or C++, so every thread owns its own line. Some processors fetch adjacent pairs of lines together, which is why 128-byte separation is sometimes used. Better still, let each thread accumulate in a local variable or register and write the shared location once at the end. Hardware performance counters can expose the problem; on Linux, the perf c2c tool reports cache lines that are frequently modified by one core and then hit by another.
Answer: A. Threads update different variables that sit in the same cache line, so the line bounces between cores and slows things down
Coherence is managed per cache line, typically 64 bytes. Even if core 0 writes only a and core 1 writes only b, when a and b share a line each write invalidates the other's copy and the line bounces between the cores. The results are correct but performance drops sharply: this is false sharing. It often occurs with arrays of per-thread counters.
Answer: B. Add padding or alignment so that each counter sits in its own cache line
With 8-byte longs and the array aligned to a 64-byte boundary, the eight counters fit in a single 64-byte line, and all threads fight over it. Even unaligned, they span only two lines, so the contention remains. Padding so each counter sits 64 bytes apart, or aligning a struct to the cache-line size, gives each thread its own line. Another effective approach is for each thread to count in a local variable and add to the shared location only once at the end.
No. The coherence protocol keeps every variable correct; false sharing only costs time, because the cache line holding the variables keeps moving between cores.
Enough that each independently written variable occupies its own cache line, usually 64 bytes. Some code uses 128 bytes because certain CPUs prefetch adjacent line pairs together.
No. Several cores can keep the same line in a shared state simultaneously; the problem appears only when at least one thread writes to the line.
False sharing appears in 4 practice questions across 2 topics, including Multicore & Synchronization, CPUs and architecture.
As an Amazon Associate I earn from qualifying purchases.
This page was drafted with AI and fact-checked by a separate AI review before publication. Our Editorial policy explains how we write and check content. If you spot an error, please tell us on our Contact page.