From 5566c5dff4d6430f704aeb66ca45615cb0df0176 Mon Sep 17 00:00:00 2001 From: Nicolas James Date: Wed, 8 Apr 2026 14:04:03 +1000 Subject: Reduce thread contention by introducing an atomic time point class - greatly reduces locking --- src/atomic_time_point.hh | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) create mode 100644 src/atomic_time_point.hh (limited to 'src/atomic_time_point.hh') diff --git a/src/atomic_time_point.hh b/src/atomic_time_point.hh new file mode 100644 index 0000000..13e62aa --- /dev/null +++ b/src/atomic_time_point.hh @@ -0,0 +1,35 @@ +#ifndef ATOMIC_TIME_POINT_HH_ +#define ATOMIC_TIME_POINT_HH_ + +#include +#include + +// The purpose of this class is to provide a simple time point which may be read +// from atomically and without locks. + +namespace low_latency { + +class AtomicTimePoint final { + private: + std::atomic count{}; + static_assert(decltype(count)::is_always_lock_free); + + public: + AtomicTimePoint(); + AtomicTimePoint(const AtomicTimePoint&) = delete; + AtomicTimePoint(AtomicTimePoint&&) = delete; + AtomicTimePoint operator=(const AtomicTimePoint&) = delete; + AtomicTimePoint operator=(AtomicTimePoint&&) = delete; + ~AtomicTimePoint(); + + public: + bool has_value() const; + + std::chrono::steady_clock::time_point get() const; + + void set(const std::chrono::steady_clock::time_point target); +}; + +} // namespace low_latency + +#endif \ No newline at end of file -- cgit v1.2.3