Skip to content

Lesson 4 of 9 · 20m

Cache Lines and False Sharing

Why two threads can make each other slow without sharing a single variable, and how padding and ownership fix line-level contention.

False sharing is the classic bug where the code is logically correct and physically hostile. Two threads update different fields, no lock protects them because none is needed, and performance still falls off a cliff. The reason is brutal: cache coherence does not know what a variable is. It knows cache lines.

Coherence invalidates lines, not fields

When one core writes a line, other cores cannot keep stale writable copies of that same line. Coherence protocols such as MESI transfer ownership and invalidate copies. If unrelated variables share a line, writes to either variable bounce the entire line between cores.

struct counters {
    long accepts;   // written by thread 1
    long rejects;   // written by thread 2
};

On a 64-byte-line machine, accepts and rejects usually live in the same line. Thread 1 writes, thread 2's copy is invalidated; thread 2 writes, ownership moves back. The program spends its time moving cache ownership rather than doing useful work.

Padding is ugly because physics is ugly

The traditional fix is to separate frequently written per-thread data by at least one cache line:

#define CACHELINE 64

struct counters {
    alignas(64) long accepts;
    alignas(64) long rejects;
};

C++ alignas(64) or compiler-specific attributes force alignment. In C, use aligned allocation plus explicit padding. Rust and C++ ecosystems often provide a CachePadded wrapper for this exact problem. Do not sprinkle it everywhere — padding increases memory traffic and can hurt when contention is absent.

Per-thread ownership beats synchronization

The deeper lesson is architectural: design so each thread owns the data it writes. Aggregate at the end.

long local_count = 0;
for (size_t i = begin; i < end; i++) {
    if (accepted(item[i])) local_count++;
}
// publish local_count once

This pattern removes both true sharing and false sharing in the hot loop. It is often better than atomics even when atomics are correct, because ownership keeps the line in one core's cache instead of making it a meeting point.

Diagnosing it in practice

False sharing shows up as scaling that gets worse as threads increase, high cache-miss or coherence traffic, and hot instructions that look innocent. Linux perf c2c can help on supported systems, but often the simplest experiment is decisive: pad the structure or give each thread its own copy and compare.

Field notes

  • Adjacent fields updated by different threads are the canonical false-sharing shape.
  • Heap allocation order can accidentally place per-thread objects on the same line; alignment alone is not a complete design.
  • Padding to 64 bytes is common, not universal — verify the line size for exotic hardware.
  • Read-mostly shared data is fine; it is repeated writes that trigger ownership transfer.
  • Do not “fix” false sharing with a global lock. That adds true contention to solve line contention.

Was this lesson helpful?

Sign in to rate