TTripleBufferedData

A lock free paradigm for sharing data between threads.

Windows
MacOS
Linux

References

Module

Chaos

Header

/Engine/Source/Runtime/Experimental/Chaos/Public/Framework/TripleBufferedData.h

Include

#include "Framework/TripleBufferedData.h"

Syntax

template<class DataType>
class TTripleBufferedData

Remarks

A lock free paradigm for sharing data between threads.

Called a triple buffer because at any point in time, there may be 3 buffers available: 1 owned by the producing thread, 1 owned by the consuming thread, and 1 waiting in an atomic variable. The third storage location enables transfer of ownership such that if you have a copy of the data, you own it without worry of contention.

Producer thread: structAnimXf{TArray<FTransform>Xf;TArray<FVector>Velocity;};structMyProducer{MyConsumerConsumer;//Ownsthetriplebuffer.AnimXf*Buffer=nullptr;//Thisfunctioniscalledrepeatedlyatsomeintervalbytheproducingthread.voidProduce(){//Getanewbufferifweneedone.if(!Buffer)Buffer=Consumer.AnimXfTripleBuffer.ExchangeProducerBuffer();//ThisclassnowhasexclusiveownershipofthememorypointedtobyBuffer.Buffer->Xf=...;Buffer->Velocity=...;//Pushthenewvaluestotheconsumer,andgetanewbufferfornexttime.Buffer=Consumer.AnimXfTripleBuffer.ExchangeProducerBuffer();}};

Consumer thread: structMyConsumer{//Inthisexampletheconsumerownsthetriplebuffer,butthat's//notarequirement.TTripleBufferedData<AnimXf>AnimXfTripleBuffer;AnimXf*Buffer=nullptr;//Thisfunctioniscalledrepeatedlyatsomeintervalbytheconsumingthread.voidConsume(){//Getanewviewofthedata,whichcanbenullorold.Buffer=AnimXfTripleBuffer.ExchangeAnimXfConsumerBuffer();//ThisclassnowhasexclusiveownershipofthememorypointedtobyBuffer.if(Buffer){...=Buffer->Xf;...=Buffer->Velocity;}}};

Constructors

Name Description

Public function

TTripleBufferedData()

Functions

Name Description

Public function

DataType ...

 

ExchangeConsumerBuffer()

Get an updated buffer for the consuming thread to read from.

Public function

DataType ...

 

ExchangeProducerBuffer()

Get a new buffer for the producing thread to write to, while at the same time making the previous buffer available to the consumer.

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