Module |
|
Header |
/Engine/Source/Runtime/Experimental/ChaosSolvers/Public/Framework/TripleBufferedData.h |
Include |
#include "Framework/TripleBufferedData.h" |
template<class DataType>
class TTripleBufferedData
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;}}};
Name | Description | |
---|---|---|
|
TTripleBufferedData() |
Name | Description | ||
---|---|---|---|
|
DataType ... |
ExchangeConsumerBuffer() |
Get an updated buffer for the consuming thread to read from. |
|
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. |