TTripleBuffer

Template for triple buffers.

Windows
MacOS
Linux

References

Module

Core

Header

/Engine/Source/Runtime/Core/Public/Containers/TripleBuffer.h

Include

#include "Containers/TripleBuffer.h"

Syntax

template<typename BufferType>
class TTripleBuffer

Remarks

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)

Constructors

Name Description

Public function

TTripleBuffer()

Default constructor.

Public function

TTripleBuffer

(
    ENoInit
)

Default constructor (no initialization).

Public function

TTripleBuffer

(
    const BufferType& InValue
)

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

Public function

TTripleBuffer

(
    BufferType(&) InBuffers
)

Create and initialize a new instance using provided buffers.

Destructors

Name Description

Public function

~TTripleBuffer()

Destructor.

Functions

Name Description

Public function

BufferType &

 

GetWriteBuffer()

Get the current write buffer.

Protected function

void

 

Initialize()

Initialize the triple buffer.

Public function Const

bool

 

IsDirty()

Check whether a new value is available for reading.

Public function

BufferType &

 

Read()

Read a value from the current read buffer.

Public function

void

 

Reset()

Reset the buffer.

Public function

const Buffer...

 

SwapAndRead()

Convenience method for fetching and reading the latest buffer.

Public function

void

 

SwapReadBuffers()

Swap the latest read buffer, if available.

Public function

void

 

SwapWriteBuffers()

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

Public function

void

 

Write

(
    const BufferType Value
)

Write a value to the current write buffer.

Public function

void

 

WriteAndSwap

(
    const BufferType Value
)

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

Enums

Name

Description

Private enum

EBufferFlag

Enumerates human readable bit values for accessing the Flags field.

Help shape the future of Unreal Engine documentation! Tell us how we're doing so we can serve you better.
Take our survey
Dismiss