Module |
|
Header |
/Engine/Source/Runtime/Core/Public/Containers/TripleBuffer.h |
Include |
#include "Containers/TripleBuffer.h" |
template<typename BufferType>
class TTripleBuffer
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
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 | Description | |
---|---|---|
|
TTripleBuffer() |
Default constructor. |
|
TTripleBuffer ( |
Default constructor (no initialization). |
|
TTripleBuffer ( |
Create and initialize a new instance with a given buffer value. |
|
TTripleBuffer ( |
Create and initialize a new instance using provided buffers. |
Name | Description | |
---|---|---|
|
~TTripleBuffer() |
Destructor. |
Name | Description | ||
---|---|---|---|
|
BufferType & |
GetWriteBuffer() |
Get the current write buffer. |
|
Initialize() |
Initialize the triple buffer. |
|
|
IsDirty() |
Check whether a new value is available for reading. |
|
|
BufferType & |
Read() |
Read a value from the current read buffer. |
|
Reset() |
Reset the buffer. |
|
|
const Buffer... |
SwapAndRead() |
Convenience method for fetching and reading the latest buffer. |
|
SwapReadBuffers() |
Swap the latest read buffer, if available. |
|
|
SwapWriteBuffers() |
Swap a new write buffer (makes current write buffer available for reading). |
|
|
Write ( |
Write a value to the current write buffer. |
|
|
WriteAndSwap ( |
Convenience method for writing the latest buffer and fetching a new one. |
Name |
Description |
|
---|---|---|
|
EBufferFlag |
Enumerates human readable bit values for accessing the Flags field. |