False sharing is a performance-degrading phenomenon in multiprocessor systems where cache coherence protocols cause unnecessary cache invalidations and data transfers between processors, despite the processors accessing different variables that happen to share the same cache line.
How False Sharing Works
-
Cache Line Structure
- Memory is transferred between main memory and caches in fixed-size blocks called "cache lines" (typically 64 or 128 bytes)
- Multiple independent variables can reside in the same cache line
-
The Problem Scenario
- Processor A writes to variable X in cache line L
- Processor B writes to variable Y in the same cache line L
- Neither processor actually accesses the other's data
- Yet cache coherence protocols treat this as true sharing
-
Performance Impact
- Cache coherence protocols (MESI, MOESI, etc.) force the line to "ping-pong" between processors
- Each write invalidates the other processor's cache line
- Processors spend time waiting for cache line transfers instead of computing
- Memory bandwidth is wasted on unnecessary transfers
- Can reduce performance by 10-100x in extreme cases
Example
// Two counters in an array
int counters[2]; // Likely on same cache line
// Thread 1 runs on Core 1
void thread1() {
for(int i = 0; i < 10000000; i++) {
counters[0]++; // Only accesses first counter
}
}
// Thread 2 runs on Core 2
void thread2() {
for(int i = 0; i < 10000000; i++) {
counters[1]++; // Only accesses second counter
}
}
Despite each thread accessing different variables, they'll experience severe performance degradation due to false sharing.
Mitigation Strategies
-
Padding
- Add padding between variables to ensure they fall on different cache lines
struct {
int counter;
char padding[64]; // Assuming 64-byte cache lines
} counters[2];
-
Alignment
- Align data structures to cache line boundaries
struct __attribute__((aligned(64))) {
int counter;
} counters[2];
-
Thread-Local Storage
- Keep frequently modified data in thread-local storage
- Merge results only when necessary
-
Data Reorganization
- Group data by access pattern rather than by logical relationship
- "Array of Structures" → "Structure of Arrays" transformation
-
Cache Line Aware Allocation
- Use specialized allocators that ensure objects don't share cache lines
- Some libraries provide cache-aligned allocation functions
-
Compiler/Language Support
- C++17 introduced
std::hardware_destructive_interference_size
- Some compilers offer alignment pragmas or attributes
-
Hardware Solutions
- Some architectures support smaller coherence granularity
- Cache line sectoring in advanced processors
Real-World Impact
False sharing is particularly problematic in:
- High-performance parallel algorithms
- Lock-free data structures
- Concurrent counters and statistics
- Thread pool implementations
- Any situation with frequent writes by multiple threads
The performance difference after addressing false sharing can be dramatic - often 2-10x speedups in heavily contended scenarios.