Skip to content
Preptima
hardConceptCodingScenarioMidSeniorStaff

Two threads write to adjacent counters and throughput collapses. What is happening and how do you fix it?

5 min readUpdated 2026-08-15

Answer

Short answer

They are false sharing: both counters sit in one cache line, so each write invalidates the other core's copy and the line ping-pongs between caches. You fix it by aligning each hot field to its own line, at the cost of footprint, and only where a hardware counter says so.

Was this answer helpful?

Likely follow-ups

  • How would you lay out a per-thread statistics array so that padding does not blow out your L2 working set?
  • What does a read-mostly field sharing a line with a frequently written one do to readers, and is that also false sharing?
  • Why can a struct that was laid out correctly start false sharing again after someone adds a field above it?
  • How do you decide between padding a field and moving it into thread-local storage that is aggregated later?

Related questions

Related Topics