Shared data is everywhere in concurrent systems: a kernel clock that a hundred threads read every millisecond, a configuration struct updated once a minute. The classic approach â a mutex or reader-writer lock â protects both sides, but often at the cost of making writers wait for readers to finish first.
A seqlock (sequence lock) flips the deal. Writers never block on readers. Instead, each write increments a shared sequence counter before and after the update. Readers snapshot the counter at the start and again at the end of their read; if the two snapshots differ, a write slipped in and the reader simply retries.
The key insight: checking consistency is cheap, and writes are rare. So instead of blocking the rare writer, you make the common reader pay a tiny retry cost on the occasions it loses the race. The Linux kernel has used this pattern for reading the system clock since 2003.
Comments
Loading comments...