Definition
A coherent snapshot is a group of related values, read from shared memory, that all belong to one version of the data, meaning one complete state that a writer produced. It is never a mix of values from before and after an update. Reading each value atomically, in one indivisible access, does not give a coherent snapshot. The whole group needs one protection: a lock, an unchanging object reached through one pointer, or a version counter that detects a concurrent writer.
The idea
A shared descriptor tells readers where a message buffer is and how long it is: a pointer field and a length field. Version A says: buffer at address 0x1000, length 4. A writer replaces it with version B: buffer at 0x2000, length 8. A reader loads the pointer and gets 0x1000. Before the reader loads the length, the writer stores both new fields. The reader then loads the length and gets 8. Each load was atomic, and each value was valid when the reader loaded it, yet the pair (0x1000, 8) never existed. Reading 8 bytes from the 4-byte buffer runs bytes past its end, an out-of-bounds read §4.5.
A read that returns parts of two different versions is called a torn read. The term also covers a single value too wide for one memory access, such as a 64-bit counter that a 32-bit processor reads as two halves, one half before and one half after an increment that carries into the upper half.
A panorama photo, taken by sweeping a phone camera across a room, shows the problem. If someone walks across the room during the sweep, that person can appear twice, because the picture was assembled piece by piece over time. A coherent snapshot is like a photograph taken at one instant: everything in it existed together. The picture stops in one respect. A coherent snapshot need not show the latest state. An older version is still coherent, as long as all values come from that same version.
How it works
Four methods give a coherent snapshot. Each works only if every writer and every reader of the group follows it.
One lock for the whole group. A mutex §9.5, a lock that lets one thread (one of several instruction sequences sharing one memory) proceed at a time, guards all fields together. Every writer holds it while changing any field, and every reader holds it while copying all fields into local variables. The reader then releases the lock and works on its copies.
One unchanging object behind one pointer. All fields live in one object that is never modified after it is published, as the immutable publication §9.24 entry describes. A writer builds a new object and switches a shared pointer to it. A reader loads the pointer once and reads every field through that one pointer value, while keeping the object alive, for example with read-copy update §9.20 (RCU) or a counted reference.
A sequence lock, also called a seqlock: a version counter stored next to the fields. A writer, one at a time, adds 1 to the counter, which makes it odd, writes the fields, and adds 1 again, which makes it even. A reader reads the counter, copies the fields, and reads the counter again. If the first value was odd, or the two values differ, a writer was active, and the reader retries. Linux uses sequence locks for its timekeeping data, which is read very often and updated at each timer tick.
One atomic word. If all fields fit into one word that the processor reads and writes in a single access, such as two 16-bit fields in one 32-bit word, one atomic load returns a coherent pair.
A sequence lock written in C or C++ needs two further details. First, the field reads must be atomic accesses; relaxed atomic loads are enough. The reader may read a field while the writer writes it, and with plain variables that is a data race §9.3, which the C11 and C++11 standards [78, 247] (the 2011 editions of the two languages) make undefined behavior §4.12; discarding the value after a failed check does not undo it. Second, memory ordering must keep the field reads between the two counter reads: the first counter read uses acquire ordering, and an acquire fence placed before the second counter read stops the field reads from moving after it. The reader also must not act on the copied values until the second check passes.
Pinning a version means holding one version for the whole length of an operation, for example one evaluation of a set of rules against one input, so that every lookup in that operation uses the same version. The pin excludes two things while the operation runs: the pinned version being freed, and the pinned version being changed. Whether a newer version can appear meanwhile depends on how the pin is held. A reader that pins by holding a lock makes every writer wait, so a new version appears only after the evaluation ends. A reader that pins with RCU makes no writer wait to publish. The writer switches the pointer at once, and operations that start later use the new version, while the pinned operation finishes on the old one; only the freeing of the old version waits, for a grace period §9.21, a wait that ends once every reader that started before the switch has finished. Holding a counted reference pins in the same way as RCU: publication goes ahead, and the old version is freed when its last reference is released.
Worked example
A sequence lock protects the descriptor from the opening. The counter is 6, and the fields hold version A: (0x1000, 4).
The reader reads the counter: 6, an even number, so no write is in progress.
The reader copies the pointer:
0x1000.The writer adds 1 to the counter (), stores
0x2000and 8, and adds 1 again ().The reader copies the length: 8.
The reader reads the counter again: 8. Since , the copy may be torn, and the reader discards (
0x1000, 8) without using it.The reader retries: counter 8, pointer
0x2000, length 8, counter 8. The two counter values are equal and even, so (0x2000, 8) is a coherent snapshot of version B.
Had the reader used the torn pair before the check, it would already have read bytes past the end of the 4-byte buffer, and the failed check afterwards cannot take that read back. The pointer field also needs its own lifetime protection: the check proves that the pair was coherent, not that the buffer at 0x2000 is still allocated when the reader uses it.
With one unchanging object instead, the shared pointer desc points to an object S0 that holds (0x1000, 4). The reader loads desc once and gets S0. The writer then builds S1, holding (0x2000, 8), and switches desc to S1. The reader reads both fields through its pointer to S0 and gets (0x1000, 4). The result is already old, because S1 exists, but it is coherent, and no retry is needed.
Stated precisely
Let the writer’s successive complete states be , and let the group have the fields . Write for the value of field in state . A read that returns the values is a coherent snapshot if there is one index such that for every from 1 to .
Freshness is a separate condition. Each of the four methods returns a version that was the current state at some instant during the read, so the read is linearizable: it behaves as if it took effect at one instant, its linearization point §8.6, between its start and its end. For the lock, that instant lies while the lock is held; for the sequence lock, between the two counter reads; for the unchanging object, at the single pointer load; for the atomic word, at the load. None of the methods keeps the snapshot current afterwards. A reader that uses its copy, or its pinned version, after a writer has produced is working on an old version. Coherence fixes which combinations of values are possible; how old a version may be when it is used needs its own rule.
Each method has its own assumptions.
Lock: every write to any field of the group, and every read that must be coherent, holds the same lock.
Unchanging object: no write to the object after its publication, one load of the pointer per snapshot, and a lifetime protection that keeps the object allocated while it is read.
Sequence lock: writers exclude each other; the field reads are atomic and ordered between the two counter reads; and the counter does not return to the same value during one read. Each write adds 2, so a 32-bit counter repeats a value only after writes. Such a counter wraparound §6.21 during one read would let a torn copy pass the check. It is an instance of the ABA problem §9.25, the failure in which a value changes and then returns to what a reader saw earlier, so that a comparison misses the change.
Atomic word: the fields form one atomic object that the processor loads and stores in one access.
Limits and common mistakes
Atomic fields are not a coherent group. Making each field atomic prevents a torn read of that one field and removes the data race, but the opening example tears across two fields although every access in it is atomic.
A coherent snapshot can be old. Pinning a version with RCU or a counted reference deliberately trades freshness for coherence: the operation finishes on the version it started with, even after a newer one is published.
Coherence is shallow. A coherent pair that contains a pointer tells the reader which buffer belonged to the version; it says nothing about whether that buffer’s contents have changed, or whether the buffer is still allocated. Data reached through a pointer must be inside the same protection, or be unchanging and kept alive.
A sequence lock can starve readers. If writes come often, a reader can keep failing its check, a form of starvation §9.18. Writers never wait for readers, so sequence locks suit small data that is read often and written rarely.
A sequence-lock reader that acts on unchecked data takes the risk of the torn pair. Following a copied pointer, or using a copied length as a bound, before the second check passes can read out of bounds or read freed memory.
A lock held for a long operation delays every writer for that long. When threads have priorities, a low-priority reader that holds the lock can delay a high-priority writer, which is the start of a priority inversion §9.16.
One writer that bypasses the protocol, for example by updating one field without taking the lock, breaks coherence for every reader.
Questions answered
Which lock or version protocol binds all fields to the same version?
Four protocols bind all fields to one version: one lock covering the whole group, one unchanging object reached through one pointer, a sequence lock, or a single atomic word; each works only if every reader and every writer of the fields follows it.
One lock for the whole group. Every writer holds the lock while it changes any of the fields, and every reader holds the same lock while it copies all of them. A separate lock per field does not work.
One unchanging object behind one pointer. The writer builds a complete new object, publishes its address with a release store, an atomic store that makes the object’s contents visible no later than the address, and never changes the object afterwards. The reader loads the pointer once, reads every field through that one address, and keeps the object alive while reading, for example with read-copy update or a counted reference.
A sequence lock. A writer makes a version counter odd before it writes and even after; a reader copies the fields between two reads of the counter and retries if the counter was odd or changed. Writers must exclude each other, the field reads must be atomic accesses in C or C++, and the reader must not use the copied values before the check passes.
One atomic word. If all fields fit in one word that the processor reads and writes in one access, one atomic load returns a coherent group.
Making each field atomic separately, or wrapping a retry check around plain non-atomic reads, does not bind the fields to one version.
Related entries: Immutable publication (§9.24), Acquire-release ordering (§9.11), Data race (§9.3), Read-copy update (RCU) (§9.20), ABA problem (§9.25), Linearization point (§8.6). Further reading: [78, 247].