Seqlock
A C++11 SeqLock implementation with reader/writer support, designed for non-blocking reads
Install / Use
/learn @htfy96/SeqlockREADME
seqlock
A C++11 SeqLock implementation with reader/writer support, designed for non-blocking reads.
Usage
#include "seqlock.hpp"
struct Foo {
int v1;
double v2;
long v3;
};
// Basic usage:
using namespace seqlock;
SeqLock<Foo> protected_data {2, 3.0};
protected_data.write(foo_1); // thread-safe. No serialization.
Foo data = protected_data.load(); // does not block write, thread-safe
// Advanced usage:
// mainly used for large structs to avoid copy
{
auto writer = protected_data.get_writer(); // move-only
// exclusive access
auto old_value = writer.read_member(&Foo::v);
writer.write_member(&Foo::v, old_value + 2);
// writer lock will be automatically released at the exit of scope
}
std::tuple<int, long> v1_and_v3 = protected_data.load_members(&Foo::v1, &Foo::v3);
A seqlock is a writer-first userspace lock. Reader doesn't block writers by retrying read based on version numbers.
Use cases:
- busy writer(s)
- a small number of writers
- starvation between writers can be tolerated
- non-blocking reads
template <typename T,
typename ReadConflictPolicy = conflict_policies::Auto,
typename WriteConflictPolicy = conflict_policies::Auto,
typename SeqCounterT = std::uint_fast32_t>
class SeqLock;
Precondition: is_trivial<T>
Config:
T: protected data, must be TrivialTypeReadConflictPolicy: Behavior when load() finds another thread writing the value. Default:conflict_policies::Auto.WriteConflictPolicy: Behavior when store() finds another thread writing the value. Default:conflict_policies::Auto.SeqCounterT: the sequence counter. Set this to a larger type if there are too many writers causing it to wrap around.
Implementation based on: Can Seqlocks Get Along with Programming Language Memory Models?, Hans-J. Boehm HP Labs
conflict_policies:
Pause: use x86 instructionpauseon conflictYield: yield to another threadRetryImmediately: no-opAuto: usePausewhen possible,Yieldotherwise
Related Skills
openhue
342.0kControl Philips Hue lights and scenes via the OpenHue CLI.
sag
342.0kElevenLabs text-to-speech with mac-style say UX.
weather
342.0kGet current weather and forecasts via wttr.in or Open-Meteo
tweakcc
1.5kCustomize Claude Code's system prompts, create custom toolsets, input pattern highlighters, themes/thinking verbs/spinners, customize input box & user message styling, support AGENTS.md, unlock private/unreleased features, and much more. Supports both native/npm installs on all platforms.
