False Sharing: Multi-threading's Cache Pitfall

False sharing occurs in multi-threaded C++ when unrelated data from different threads accidentally reside in the same cache line (typically 64 bytes), forcing CPUs to unnecessarily invalidate and refresh caches, leading to significant performance degradation despite data not being truly shared.

The top 3

  1. Top 3 Tactics to Prevent False Sharing: Developers commonly use padding to separate data onto distinct cache lines, align variables to cache line boundaries, and employ thread-local storage for independent data to avoid false sharing.
  2. False Sharing's Severe Performance Penalties: False sharing can lead to order-of-magnitude performance degradation and significant slowdowns by forcing excessive cache line invalidations and reloads across CPU cores, even for logically independent data.
  3. Top 3 Data Structures Prone to False Sharing: Common culprits for false sharing include arrays of thread-local counters, densely packed structs with fields accessed by different threads, and small global variables placed adjacently in memory.

Sources

Open the full topic