6.1.10 · D3Parallelism & Multicore

Worked examples — False sharing problem

2,767 words13 min readBack to topic

Before anything else, let's pin down the one measurement everything rests on.


The scenario matrix

Every false-sharing situation is one of these case classes. Each worked example is tagged with the cell it fills.

# Case class The question it answers Example
A Adjacent, tightly packed Do neighbouring array elements share a line? Ex 1
B Boundary straddle What if one variable spans two lines? Ex 2
C Zero-cost / no sharing When does spacing already save us? Ex 3
D Degenerate: single writer Is it still false sharing with one core? Ex 4
E Padding fix, exact How much padding is just enough? Ex 5
F Over-alignment trap Why 8-byte align fails, 64 works Ex 6
G Limiting case: many cores How does cost scale as cores → large? Ex 7
H Real-world word problem Timing a counter benchmark end-to-end Ex 8
I Exam twist: partial sharing Some cores clash, some don't — count them Ex 9

We build the same tool for all of them: compute for each variable, then check which indices collide.

Figure — False sharing problem

Worked examples


Recall Self-test

All hex→decimal, then . Do two variables 40 bytes apart starting at a line boundary share a line? ::: Yes — , both floor to the same index. How many padding bytes isolate a 4-byte int on a 64-byte line? ::: bytes. Does 8-byte alignment stop false sharing? ::: No — 8 variables still pack into one 64-byte line. Is there false sharing if only one core ever writes the line? ::: No — false sharing needs writes from ≥ 2 different cores. Why does adding cores not help a false-shared counter loop? ::: The single Modified line serializes all writes; parallelism collapses.

Related building blocks: Cache coherence protocols · Cache line size and alignment · Atomic operations · Lock-free data structures · Memory allocators · NUMA architectures · Multicore scaling · back to the parent topic.