FALSE SHARING

Two threads, two variables, one cache line

L214 MINSYSTEMS & LOW-LEVEL
PREDICT

Core A writes a, core B writes b, alternating, 8 times each (16 writes). a is at offset 0 and b at offset 8 of a 64-byte-aligned struct, so both sit in one 64-byte cache line. How many times does that line move between the cores?

Caches do not track variables. They track 64-byte lines. To write any byte of a line, a core needs the whole line in the Modified state, and the coherence protocol takes it away from every other core first.

Put two counters next to each other and they share a line. Core A writes a and owns the line. Core B writes b and pulls the line over, invalidating A’s copy. A writes again and pulls it back. Every write is a cross-core transfer, tens of cycles instead of one. The fix is layout: put each thread’s data on its own line.

▶ SIMULATION
CORE A (writes a)
no lines cached
cycles spent 0
—
STRUCT LAYOUT (64-byte lines)
padding = 0 bytes
CORE B (writes b)
no lines cached
cycles spent 0
—
writes0
cross-core transfers0 (each invalidated the other core's copy)
hits · memory fills0 · 0
mean cycles per write—
✓ cross-core transfers ≤ 2 for 16 writes
SCHEDULE
01[Core A]layout: a at 0, b right after a + PADDING
02[Core A]a++ (write #1)
03[Core B]b++ (write #1)
04[Core A]a++ (write #2)
05[Core B]b++ (write #2)
06[Core A]a++ (write #3)
07[Core B]b++ (write #3)
08[Core A]a++ (write #4)
09[Core B]b++ (write #4)
10[Core A]a++ (write #5)
11[Core B]b++ (write #5)
12[Core A]a++ (write #6)
13[Core B]b++ (write #6)
14[Core A]a++ (write #7)
15[Core B]b++ (write #7)
16[Core A]a++ (write #8)
17[Core B]b++ (write #8)
18[Core A]report
Start of packed (uses padding) scenario. 18 steps.
struct counters {
    long a;   // written only by thread A
    long b;   // written only by thread B, same 64-byte line as a
};

Watch the line states in each core and the invariant strip. In PACKED with padding 0, the line flips between M and I on every step and the strip goes red on the second round. In alignas(64), each core fills its own line once and every later write is a 1-cycle hit.

TUNE IT NOT YET

Padding wastes memory, so use as little as works. Find the smallest PADDING that puts b on a different cache line from a and keeps cross-core transfers within budget.

GOAL: transfers ≤ 2 with the least padding that achieves it

FIX IT NOT YET

Write layout(fields, lineSize). Each field is { name, size, writer }; return an object mapping each name to a byte offset. Rules: each offset is a multiple of the field's size; fields never overlap; fields written by different threads never share a cache line; fields written by the same thread share lines where they fit, so you do not waste a line per field.

Mental model:

the unit of sharing is the cache line, not the variable. Two threads that write anything on the same line are contending, whatever your source code says. Lay out data by who writes it: each writer gets its own line, and fields that one thread writes stay together.

IN PRODUCTION

Per-thread counters and statistics, the head and tail indexes of a queue, lock arrays, and per-core shards stored in one array. Java has @Contended, C++ has std::hardware_destructive_interference_size, the LMAX Disruptor pads its sequence counters, and Linux tools like perf c2c find the hot line.

IN INTERVIEWS

“Why does this multi-threaded counter get slower with more threads?” Describe cache-line granularity, how a write invalidates the other core's copy, and the padding or per-core sharding fix. Mention that the cost is coherence traffic, not a correctness bug: the counts stay right.

RECALL CARDS Tap to flip. They join your review deck when you mark the lesson complete.
NEXT: TAIL LATENCY →