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.

Field: Multicore & Synchronization

Japanese name: 偽共有

Why coherence works per line, not per variable

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.

False sharing vs. true sharing

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.

Typical culprits

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.

How to fix and find it

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.

Try it: questions about False sharing

Which describes false sharing in a multithreaded program?

  1. AThreads update different variables that sit in the same cache line, so the line bounces between cores and slows things down
  2. BTwo threads update the same variable without synchronization and corrupt it
  3. CThreads wait on each other's locks and stop
Show answer and explanation

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.

False sharing occurs with an array of per-thread counters such as "long cnt[8];". What is the most suitable fix?

  1. AChange the counters from long to int to shrink the array
  2. BAdd padding or alignment so that each counter sits in its own cache line
  3. CIncrease the number of threads to spread out the waiting
Show answer and explanation

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.

FAQ

Does false sharing cause wrong results?

No. The coherence protocol keeps every variable correct; false sharing only costs time, because the cache line holding the variables keeps moving between cores.

How much padding do I need to avoid false sharing?

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.

Can read-only shared data cause false sharing?

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.

Questions that mention False sharing (2)

False sharing appears in 4 practice questions across 2 topics, including Multicore & Synchronization, CPUs and architecture.

  1. What is the typical size of a cache line (cache block) in many CPUs?
  2. Which is NOT a reason a parallel program fails to scale as cores are added?

Books on Multicore & Synchronization Ad

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.

Back to the glossary