Class thl::TripleBuffer#

template<typename T>
class TripleBuffer#

Wait-free single-producer, single-consumer “latest value” mailbox.

The reader always gets the most recent complete value; values it did not pick up are overwritten. Exactly one writer thread and one reader thread, which may be the same. T must be trivially copyable.

The three slots and the two sides’ indices each start a cache line of their own (core::k_cache_line_size), so a TripleBuffer is at least six of them.

Public Functions

TripleBuffer() = default#
inline explicit TripleBuffer(const T &initial)#
TripleBuffer(const TripleBuffer&) = delete#
TripleBuffer &operator=(const TripleBuffer&) = delete#
TripleBuffer(TripleBuffer&&) = delete#
TripleBuffer &operator=(TripleBuffer&&) = delete#
~TripleBuffer() = default#
inline void write(const T &value) noexcept#

Publish a copy of value.

inline T &write_buffer() noexcept#

The writer’s slot, to fill in place before publish().

Its content is a stale value from some earlier publication, not the last one written.

inline void publish() noexcept#

Publish write_buffer().

inline bool has_new() const noexcept#

True if a value was published that neither read() nor update() has taken yet (a hint: the writer may publish right after).

inline bool read(T &out) noexcept#

Copy the latest value into out if one was published since the last read().

Returns false (and leaves out untouched) otherwise.

inline bool update() noexcept#

Take the latest publication (if any) into the reader’s slot.

Returns:

true if it is new. latest() then refers to it until the next call.

inline const T &latest() const noexcept#

The reader’s current value (the default/initial value before the first update()).