Independent objects can share a line
False sharing occurs when CPUs update logically independent data that happens to occupy the same cache line, creating unnecessary coherence traffic. The objects need not represent the same value. Caches ordinarily track ownership at a granularity larger than many individual variables, so software ownership boundaries can differ from hardware coherence boundaries. A correctly synchronized program can suffer from this performance effect without a data race. Distinguishing false sharing from true sharing is important: one concerns unrelated objects placed together, while the other reflects workers genuinely communicating through the same data.
Trace two neighbouring counters
Assume a 64-byte cache line containing two neighbouring eight-byte counters, A and B. CPU 0 repeatedly updates A, while CPU 1 repeatedly updates B. Each writer needs a suitable ownership state for the line, so the line can move between CPUs despite the counters’ logical independence. Separating the counters can reduce that unnecessary transfer. The 64-byte size is an assumption for this example, not a universal promise for all machines. Check actual line boundaries and allocation layout before claiming that two fields are, or are not, sharing a coherence unit.
Padding has a footprint cost
Padding counters apart increases memory use and can hurt cache or TLB efficiency elsewhere. It is not necessary to separate every read-mostly object mechanically. Keeping fields together can improve spatial locality when one CPU uses them together. Choose layout using update frequency, access ownership and the actual parallel phase. If every worker increments one shared counter, the contention is true sharing; merely padding that counter does not remove the common update point. Per-worker accumulation followed by a merge may address that different problem, but changes when aggregate values are available.
Verify the mechanism before tuning
Compare implementations that perform equivalent work with the same worker count and differ in layout. Confirm that the compiler did not remove the updates and that workers actually run on multiple CPUs. Tools such as Linux perf c2c can help identify shared-line traffic, subject to hardware support and permissions. A shorter run after padding is useful evidence, but connect it to the changed layout and coherence behaviour. “More threads are slower” is not a sufficient diagnosis, since scheduling, locking, memory bandwidth and other bottlenecks can produce the same symptom.