← Lessons

quiz vs the machine

Platinum1760

Concurrency

The False Sharing Cache Line

Independent variables on one cache line ping pong between cores.

5 min read · advanced · beat Platinum to climb

The False Sharing Cache Line

False sharing is a performance bug where two threads touch different variables that happen to live on the same cache line. Logically they share nothing, but the hardware tracks ownership at cache line granularity, usually sixty four bytes, so the cores fight over the line anyway.

Caches keep coherence per line. When core A writes its variable, the protocol invalidates that line in every other cache. If core B then writes its own variable on the same line, it must reload and take exclusive ownership, invalidating A in turn. The line ping pongs between caches on every write even though the actual data is independent.

  • No correctness bug The results are right, only the speed collapses.
  • Invisible in source The variables look unrelated, the collision is purely about physical layout.
  • Scales badly Adding cores makes it worse, since more cores contend for the same line.

A classic example is an array of per thread counters packed tightly together. Each thread updates its own slot, yet they all sit on a few shared lines and the program crawls. The cure is to ensure each hot variable occupies its own line.

Key idea

False sharing happens when unrelated variables share a cache line, forcing the line to bounce between cores on every write and crushing throughput.

Check yourself

Answer to earn rating on the learn ladder.

1. Why does false sharing hurt performance despite the variables being unrelated?

2. What is a hallmark of a false sharing problem?