C multithreading experiment: padding counters to 64 bytes yields 3-4x speedup via false sharing fix

cneuralnetwork · x · 2026-10-23

The author ran a C multithreading experiment: two threads each incrementing separate counters 20 million times ran 3-4x faster after simply adding padding between the counters.

The cause is false sharing — CPUs maintain cache coherence at the cache-line level (typically 64 bytes), not per variable. Even though the threads touched different counters, they kept invalidating each other's cache line; separating them in memory removed most of the overhead. Five benchmark rounds show consistent 2.7x-4.6x speedups with identical final counts.

Original post →

More from Infra

Infra channel →