Class thl::SeqLock#
-
class SeqLock#
Sequence counter of a seqlock over data the caller keeps in atomics.
Lets a reader see a batch of stores completely or not at all without ever waiting: a reader that overlaps a write discards what it read (keeps its previous copy) instead of retrying. The protected data must itself be atomics (relaxed loads and stores are enough), so racing reads are not data races.
One writer at a time: the caller serialises writers (e.g. under a mutex). Any number of readers.
// Writer lock.write_begin(); a.store(1, std::memory_order_relaxed); b.store(2, std::memory_order_relaxed); lock.write_end(); // Reader (real-time safe) if (const auto token = lock.read_begin()) { const int x = a.load(std::memory_order_relaxed); const int y = b.load(std::memory_order_relaxed); if (lock.read_valid(*token)) { use(x, y); } // else keep the last copy }
Public Functions
-
inline void write_begin() noexcept#
Writer: call before the stores of a batch (the count turns odd).
-
inline void write_end() noexcept#
Writer: call after the stores (the count turns even and publishes them).
-
inline std::optional<uint32_t> read_begin() const noexcept#
Reader: a token to check after the loads, or nullopt while a write is in progress.
-
inline bool read_valid(uint32_t token) const noexcept#
Reader: true if no write overlapped the loads since read_begin() returned
token.
-
inline void write_begin() noexcept#