What is Cache coherency?
In a multicore CPU, the mechanism that keeps copies of the same address in different cores' caches consistent, for example by invalidating other copies when one core writes.
Japanese name: キャッシュコヒーレンシ
In a multicore CPU, the mechanism that keeps copies of the same address in different cores' caches consistent, for example by invalidating other copies when one core writes.
Japanese name: キャッシュコヒーレンシ
In a multicore CPU each core has its own cache, and copies of the same address can sit in several caches at once. If one core changes its copy while another keeps reading an old copy, the program runs on wrong values. Cache coherency is the mechanism by which the hardware prevents this automatically, so that software sees what looks like a single memory.
Each cache records a state for every line, such as whether other cores also hold it and whether this core has modified it. Before a core writes, it notifies the others so that they invalidate their copies of that line. A core whose copy was invalidated fetches the new contents the next time it needs them. The best-known set of states is the MESI protocol. Notification is done either by broadcasting to all cores (snooping) or by recording which cores hold a copy and telling only those (a directory), and systems with many cores use the latter.
When several cores write to the same line in turn, each write invalidates and refetches it, so the line bounces between cores and everything slows down. The same happens with separate variables that share one line (64 bytes, for example). This is false sharing, and it occurs, for instance, when per-thread counters sit next to each other. Adding padding between the variables or aligning them to line boundaries puts them on separate lines and avoids it.
Answer: A. Core 1 reads the stale value left in its own cache
In a multicore CPU, each core's cache can hold a copy of the same address. If core 0 updates x only in its own cache, core 1's cache keeps the old value and core 1 reads it. A cache coherence mechanism, for example by invalidating other cores' copies on a write, ensures that every core sees the latest value.
Answer: B. Modified, Exclusive, Shared, Invalid
In MESI, each cache line is in one of four states: Modified (changed and held only by this core), Exclusive (unchanged and held only by this core), Shared (several cores hold the same contents) or Invalid. The state changes whenever the core reads or writes, or learns of other cores' reads and writes, which prevents copies of the same address from disagreeing.
Not for correctness; the hardware keeps the copies consistent automatically. For performance, avoid placing data that several threads write frequently on the same line.
Yes. Coherency only guarantees that copies of the same address do not disagree. Protecting a sequence of steps such as read, modify and write, and dealing with the reordering caused by the CPU delaying its writes, are separate problems handled with locks or atomic operations.
Cache coherency appears in 5 practice questions across 2 topics, including Multicore & Synchronization, I/O Interfaces & Buses.
As an Amazon Associate I earn from qualifying purchases.
This page was drafted with AI and fact-checked by a separate AI review before publication. Our Editorial policy explains how we write and check content. If you spot an error, please tell us on our Contact page.