UDocumentation UE5.7 10.02.2026 (Source)
API documentation for Unreal Engine 5.7
TTripleBuffer< BufferType > Class Template Reference

#include <TripleBuffer.h>

Public Member Functions

 TTripleBuffer ()
 
 TTripleBuffer (ENoInit)
 
 TTripleBuffer (const BufferType &InValue)
 
 TTripleBuffer (BufferType(&InBuffers)[3])
 
 ~TTripleBuffer ()
 
bool IsDirty () const
 
BufferType & Read ()
 
void SwapReadBuffers ()
 
BufferType & GetWriteBuffer ()
 
void SwapWriteBuffers ()
 
void Write (const BufferType Value)
 
void Reset ()
 
const BufferType & SwapAndRead ()
 
void WriteAndSwap (const BufferType Value)
 

Protected Member Functions

void Initialize ()
 

Detailed Description

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
BufferTypeThe type of buffered items.

Constructor & Destructor Documentation

◆ TTripleBuffer() [1/4]

template<typename BufferType >
TTripleBuffer< BufferType >::TTripleBuffer ( )
inline

Default constructor.

◆ TTripleBuffer() [2/4]

template<typename BufferType >
TTripleBuffer< BufferType >::TTripleBuffer ( ENoInit  )
inlineexplicit

Default constructor (no initialization).

◆ TTripleBuffer() [3/4]

template<typename BufferType >
TTripleBuffer< BufferType >::TTripleBuffer ( const BufferType &  InValue)
inlineexplicit

Create and initialize a new instance with a given buffer value.

Parameters
InValueThe initial value of all three buffers.

◆ TTripleBuffer() [4/4]

template<typename BufferType >
TTripleBuffer< BufferType >::TTripleBuffer ( BufferType(&)  InBuffers[3])
inline

Create and initialize a new instance using provided buffers.

The elements of the provided items array are expected to have the following initial contents: 0 = Temp 1 = Write 2 = Read

Parameters
InBuffersThe buffer memory to use.

◆ ~TTripleBuffer()

template<typename BufferType >
TTripleBuffer< BufferType >::~TTripleBuffer ( )
inline

Destructor.

Member Function Documentation

◆ GetWriteBuffer()

template<typename BufferType >
BufferType & TTripleBuffer< BufferType >::GetWriteBuffer ( )
inline

Get the current write buffer.

Returns
Reference to write buffer.

◆ Initialize()

template<typename BufferType >
void TTripleBuffer< BufferType >::Initialize ( )
inlineprotected

Initialize the triple buffer.

◆ IsDirty()

template<typename BufferType >
bool TTripleBuffer< BufferType >::IsDirty ( ) const
inline

Check whether a new value is available for reading.

Returns
true if a new value is available, false otherwise.

◆ Read()

template<typename BufferType >
BufferType & TTripleBuffer< BufferType >::Read ( )
inline

Read a value from the current read buffer.

Returns
Reference to the read buffer's value.
See also
SwapRead, Write

◆ Reset()

template<typename BufferType >
void TTripleBuffer< BufferType >::Reset ( )
inline

Reset the buffer.

◆ SwapAndRead()

template<typename BufferType >
const BufferType & TTripleBuffer< BufferType >::SwapAndRead ( )
inline

Convenience method for fetching and reading the latest buffer.

Returns
Reference to the buffer.
See also
SwapRead, Read, WriteAndSwap

◆ SwapReadBuffers()

template<typename BufferType >
void TTripleBuffer< BufferType >::SwapReadBuffers ( )
inline

Swap the latest read buffer, if available.

Will not perform a back-buffer swap if no new data is available (dirty flag = false).

See also
SwapWrite, Read

◆ SwapWriteBuffers()

template<typename BufferType >
void TTripleBuffer< BufferType >::SwapWriteBuffers ( )
inline

Swap a new write buffer (makes current write buffer available for reading).

See also
SwapRead, Write

◆ Write()

template<typename BufferType >
void TTripleBuffer< BufferType >::Write ( const BufferType  Value)
inline

Write a value to the current write buffer.

Parameters
ValueThe value to write.
See also
SwapWrite, Read

◆ WriteAndSwap()

template<typename BufferType >
void TTripleBuffer< BufferType >::WriteAndSwap ( const BufferType  Value)
inline

Convenience method for writing the latest buffer and fetching a new one.

Parameters
ValueThe value to write into the buffer.
See also
SwapAndRead, SwapWrite, Write

The documentation for this class was generated from the following file: