False Sharing

False sharing happens when two threads on different cores touch unrelated variables that share a cache line, so each write invalidates the line in the other core’s private cache. From ECE459 L08.

Why?

Cache coherency (MESI) works at the granularity of a cache line, not individual variables. The hardware can’t tell that the two threads are writing disjoint bytes, so every write forces the other core to re-fetch the whole line.

Isn't this only a problem across different cores?

Yes. Two threads pinned to the same core share the same L1 cache, so hitting the same line is just a normal cache hit — no coherency traffic.

False sharing needs the line to live in separate private caches, with at least one core writing so the protocol has to invalidate the others.

char a[10];
char b[10];

The arrays don’t overlap but are almost certainly allocated next to each other. If thread 1 (core 0) writes to a and thread 2 (core 1) reads b, the line bounces between caches even though the variables don’t logically share.

Fixes:

  • Heap-allocate (usually but not guaranteed to land on distinct lines)
  • Pad the arrays or struct so each hot field sits on its own line
  • L08’s plot shows a ~5× speedup at separation 51 bytes; wasting some space is worth it

Putting elements in a struct and padding the struct also works, and is future-friendly.