Navigation
Unreal Engine C++ API Reference > Runtime > Chaos > Framework
References
Module
Chaos
Header
/Engine/Source/Runtime/Experimental/Chaos/Public/Framework/TripleBufferedData.h
Include
#include "Framework/TripleBufferedData.h"
Syntax
template<class DataType>
class TTripleBufferedData
Copy full snippet
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{TArrayXf;TArrayVelocity;};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.TTripleBufferedDataAnimXfTripleBuffer;AnimXf*Buffer=nullptr;//Thisfunctioniscalledrepeatedlyatsomeintervalbytheconsumingthread.voidConsume(){//Getanewviewofthedata,whichcanbenullorold.Buffer=AnimXfTripleBuffer.ExchangeAnimXfConsumerBuffer();//ThisclassnowhasexclusiveownershipofthememorypointedtobyBuffer.if(Buffer){...=Buffer->Xf;...=Buffer->Velocity;}}};
Constructors
Type
Name
Description
Functions
Type
Name
Description
DataType *
Get an updated buffer for the consuming thread to read from.
DataType *
Get a new buffer for the producing thread to write to, while at the same time making the previous buffer available to the consumer.