Navigation
API > API/Runtime > API/Runtime/Core
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)
| Name | TTripleBuffer |
| Type | class |
| Header File | /Engine/Source/Runtime/Core/Public/Containers/TripleBuffer.h |
| Include Path | #include "Containers/TripleBuffer.h" |
Syntax
template<typename BufferType>
class TTripleBuffer
Constructors
| Name | Remarks | Include Path | Unreal Specifiers |
|---|---|---|---|
| Default constructor. | Containers/TripleBuffer.h | ||
TTripleBuffer
(
ENoInit |
Default constructor (no initialization). | Containers/TripleBuffer.h | |
TTripleBuffer
(
const BufferType& InValue |
Create and initialize a new instance with a given buffer value. | Containers/TripleBuffer.h | |
TTripleBuffer
(
BufferType(&) InBuffers |
Create and initialize a new instance using provided buffers. | Containers/TripleBuffer.h | |
TTripleBuffer
(
const TTripleBuffer& |
Hidden copy constructor (triple buffers cannot be copied). | Containers/TripleBuffer.h |
Destructors
| Name | Remarks | Include Path | Unreal Specifiers |
|---|---|---|---|
~TTripleBuffer() |
Destructor. | Containers/TripleBuffer.h |
Variables
Protected
| Name | Type | Remarks | Include Path | Unreal Specifiers |
|---|---|---|---|---|
| Buffers | BufferType * | The three buffers. | Containers/TripleBuffer.h | |
| Flags | int32 volatile | Buffer access flags. | Containers/TripleBuffer.h | |
| OwnsMemory | bool | Whether this instance owns the buffer memory. | Containers/TripleBuffer.h |
Functions
Public
| Name | Remarks | Include Path | Unreal Specifiers |
|---|---|---|---|
BufferType & GetWriteBuffer() |
Get the current write buffer. | Containers/TripleBuffer.h | |
bool IsDirty() |
Check whether a new value is available for reading. | Containers/TripleBuffer.h | |
BufferType & Read () |
Read a value from the current read buffer. | Containers/TripleBuffer.h | |
void Reset() |
Reset the buffer. | Containers/TripleBuffer.h | |
const BufferType & SwapAndRead () |
Convenience method for fetching and reading the latest buffer. | Containers/TripleBuffer.h | |
void SwapReadBuffers () |
Swap the latest read buffer, if available. | Containers/TripleBuffer.h | |
void SwapWriteBuffers () |
Swap a new write buffer (makes current write buffer available for reading). | Containers/TripleBuffer.h | |
void Write
(
const BufferType Value |
Write a value to the current write buffer. | Containers/TripleBuffer.h | |
void WriteAndSwap
(
const BufferType Value |
Convenience method for writing the latest buffer and fetching a new one. | Containers/TripleBuffer.h |
Protected
| Name | Remarks | Include Path | Unreal Specifiers |
|---|---|---|---|
void Initialize() |
Initialize the triple buffer. | Containers/TripleBuffer.h |
Static
| Name | Remarks | Include Path | Unreal Specifiers |
|---|---|---|---|
static int32 SwapReadWithTempFlags
(
int32 Flags |
Swaps the read and temp buffer indices in the Flags field. | Containers/TripleBuffer.h | |
static int32 SwapWriteWithTempFlags
(
int32 Flags |
Swaps the write and temp buffer indices in the Flags field, and sets the dirty bit. | Containers/TripleBuffer.h |
Operators
| Name | Remarks | Include Path | Unreal Specifiers |
|---|---|---|---|
TTripleBuffer & operator=
(
const TTripleBuffer& |
Hidden copy assignment (triple buffers cannot be copied). | Containers/TripleBuffer.h |