template<
typename BufferType>
class TTripleBuffer< BufferType >
Template for triple buffers.
This template implements a lock-free triple buffer that can be used to exchange data between two threads that are producing and consuming at different rates. Instead of atomically exchanging pointers to the buffers, we atomically update a Flags register that holds the indices into a 3-element buffer array.
The three buffers are named as follows:
- Read buffer: This is where Read() will read the latest value from
- Write buffer: This is where Write() will write a new value to
- Temp buffer: This is the second back-buffer currently not used for reading or writing
Please note that reading and writing to the buffer does not automatically swap the back-buffers. Instead, two separate methods, SwapReadBuffers() and SwapWriteBuffers() are provided. For convenience, we also provide SwapAndRead() and WriteAndSwap() to update and swap the buffers using a single method call.
A dirty flag indicates whether a new value has been written and swapped into the second back-buffer and is available for reading. It can be checked using the IsDirtyMethod(). As an optimization, SwapReadBuffers() and SwapAndRead() will not perform a back-buffer swap if no new data is available.
This class is thread-safe in single-producer, single-consumer scenarios.
Based on ideas and C code in "Triple Buffering as a Concurrency Mechanism" (Reddit.com)
- Parameters
-
| BufferType | The type of buffered items. |