What is False Sharing? #
False sharing occurs when multiple threads modify independent variables that reside in the same cache line, leading to unnecessary cache invalidation and performance degradation. This issue is often subtle and may not be immediately apparent in code, but it can significantly impact performance in multi-threaded programs.
Why is it Called ‘False’ Sharing? #
- What is shared? A cache line containing different variables used by multiple threads.
- Why ‘false’? The variables are not logically shared in terms of their use by different threads, but because they occupy the same cache line, any modification by one thread forces an update across multiple cores, causing inefficient cache invalidations and increased cache coherence traffic.
When Does False Sharing Happen? #
False sharing occurs in multi-threaded programs due to the interaction between the following factors:
- Adjacent variables: When threads modify independent variables stored adjacently in memory, they may end up in the same cache line.
- Cache coherence protocols: In systems with cache coherence protocols (e.g., MESI), even though each core has its own private cache (L1/L2), any modification in one core’s cache must be reflected across all cores to maintain consistency. This forces unnecessary cache invalidations and cache misses when a variable in the same cache line is modified by different threads.
- Private L1/L2 caches: Although caches are private to each core, the cache coherence protocol ensures consistency across caches, triggering updates or invalidations in other cores’ caches, which can degrade performance due to false sharing.
This combination of adjacent data in the same cache line and the behavior of cache coherence protocols results in costly synchronization and performance degradation, even when the variables are independent.