concept Servers & hardware
False sharing and cache-line contention
Two threads can update different counters yet contend because write ownership applies to the cache line, not the counter. If their cores take turns writing one line, ownership can move back and forth.
What it means
Separate variables can require ownership changes when different cores write the same cache line. This is false sharing: each core writes only its own field, but write access is coordinated for the line as a whole. A write can also interfere with another core reading a different field on that line. Linux kernel: False Sharing
Try it
The simulation uses two 8-byte counters: core 0 writes A, and core 1 writes B. It uses synthetic inputs, not measurements of your hardware or service.
False sharing and cache-line contention
Move two synthetic counters onto the same or separate cache lines; step through ownership transfers and show which write caused each transfer.
Synthetic inputs only. This is not a measurement of your hardware or service.
3 writes changed an already known line owner. Cold acquisition is not counted as a transfer.
Line placement and ownership
- Counter A line
- 0
- Counter B line
- 0
- Known-owner changes
- 3
| Write | Counter | Line | Owner before | Owner after | Owner changed |
|---|---|---|---|---|---|
| 1 | A | 0 | Unowned | 0 | No |
| 2 | B | 0 | 0 | 1 | Yes |
| 3 | A | 0 | 1 | 0 | Yes |
| 4 | B | 0 | 0 | 1 | Yes |
Evaluated evidence
- Cache-line size (bytes)
- 64
- Counter A byte offset (bytes)
- 0
- Counter B byte offset (bytes)
- 8
- Evaluated counter-writer sequence
- A · B · A · B
Simplified exclusive write ownership, not a complete coherence protocol or traffic/timing estimate. Padding is not a universal recommendation.
About this model
Every input is local synthetic evidence. Numbers stay as raw text until this model validates them. The shown evaluated values come from the same result as the explanation.
Presets replace the entire scenario. Reset restores all starting fields, scenario selection and model state; it does not change the open or closed state of this help or article FAQ.
No external request, device command, stored-input history or measurement is performed.
Start with Cache-line size (bytes) set to 64, Counter A byte offset to 0, and Counter B byte offset to 8. Offsets start at the beginning of line 0. To find a counter’s line ID, divide its offset by the line size and round down. A occupies bytes 0–7 and B occupies bytes 8–15: they do not overlap, but both are on line 0.
With Writer sequence set to A B A B, follow the owner of line 0:
- Core 0 writes A. It acquires the line, but there was no previous owner, so this first write does not count as a transfer.
- Core 1 writes B. The owner changes from core 0 to core 1 even though B’s bytes are separate from A’s.
- Core 0 writes A again, taking ownership back from core 1.
- Core 1 writes B again, taking ownership back from core 0.
Known-owner changes is 3, at writes 2, 3, and 4. Choose Place counter B on a separate line to change only B’s offset to 64. Now A is on line 0 and B is on line 1. Each core keeps its own line after its first write, so the same sequence produces 0 transfers. To test write order instead, put B back at 8 and try A A B B. Only write 3, the first B, changes the owner.
The model accepts line sizes of 32 or 64 bytes and sequences of 1–32 space-separated A or B writes. Choose Inspect the evidence boundary to leave the line size unknown: the transfer count is then unknown, not zero. Offsets must be whole-number decimal strings from 0 through 1024, aligned to 8 bytes; 8.000000000000000001 is invalid, not rounded to 8. Giving both counters offset 0 is also invalid: overlapping byte ranges are not false sharing. Reset restores the default inputs and scenario, returns the step and virtual time to zero, and clears errors, history, and the comparison.
In practice
A capacity miss concerns lines that no longer fit in the cache. Cornell CS3410: Caches No line had to be evicted for lack of space in the example; the three transfers came from different cores taking turns writing one line. Both mechanisms can occur in a workload. The simulation tracks exclusive write ownership only: its count is neither a tally of hardware coherence messages nor a timing estimate.
Before proposing padding, check the actual line size, where the fields fall relative to a line boundary, and which cores read or write them. If you change the layout, verify that it helps the workload; a change can move the bottleneck rather than remove it. Linux kernel: False Sharing Nonoverlapping addresses tell you the writers update different bytes. They do not tell you the writers have independent cache-line ownership.
Frequently asked questions
Editorial questions based on the listed sources and explicitly labeled local examples.
Can false sharing involve a reader?
Yes. A write to one field can interfere with another core reading a different field in the same line. The main simulation uses two writers to keep ownership changes explicit.
Is padding always worthwhile?
Not automatically. A layout change must be checked for side effects and actual benefit; moving one bottleneck can expose another.
Does a transfer count measure time?
No. A counted owner change is one event in this toy model. It is not a measured duration or a complete count of hardware coherence messages.
How is this different from a capacity miss?
A capacity miss concerns lines no longer fitting in the cache. False sharing concerns accesses to separate data that share a coherence line. The mechanisms can coexist.
Sources & model boundaries
Primary references used for this explanation. The interactive example is an educational model, not a live check.
- Linux kernel: False Sharing
What is false sharing; mitigation and cautions. Different fields in one cache line may interfere when at least one access writes; write/read cases also occur. Mitigation needs verification and can move a bottleneck.
Checked .
- Cornell CS3410: Caches
Hits & Misses; Locality; Fully Associative Cache; Replacement Policies; Three Categories of Misses. A miss may be cold, capacity-related or conflict-related. LRU is an explicit replacement rule for a teaching cache, not a universal CPU policy.
Checked .