engine
 
载入中...
搜索中...
未找到
Public 成员函数 | Protected 成员函数 | 所有成员列表
TTripleBuffer< BufferType > 模板类 参考

Template for triple buffers. 更多...

#include <TripleBuffer.h>

Public 成员函数

BufferType & GetWriteBuffer ()
 Get the current write buffer. 更多...
 
bool IsDirty () const
 Check whether a new value is available for reading. 更多...
 
BufferType & Read ()
 Read a value from the current read buffer. 更多...
 
void Reset ()
 Reset the buffer.
 
const BufferType & SwapAndRead ()
 Convenience method for fetching and reading the latest buffer. 更多...
 
void SwapReadBuffers ()
 Swap the latest read buffer, if available. 更多...
 
void SwapWriteBuffers ()
 Swap a new write buffer (makes current write buffer available for reading). 更多...
 
 TTripleBuffer ()
 Default constructor.
 
 TTripleBuffer (BufferType(&InBuffers)[3])
 Create and initialize a new instance using provided buffers. 更多...
 
 TTripleBuffer (const BufferType &InValue)
 Create and initialize a new instance with a given buffer value. 更多...
 
 TTripleBuffer (ENoInit)
 Default constructor (no initialization).
 
void Write (const BufferType Value)
 Write a value to the current write buffer. 更多...
 
void WriteAndSwap (const BufferType Value)
 Convenience method for writing the latest buffer and fetching a new one. 更多...
 
 ~TTripleBuffer ()
 Destructor.
 

Protected 成员函数

void Initialize ()
 Initialize the triple buffer.
 

详细描述

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:

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)

参数
BufferTypeThe type of buffered items.

构造及析构函数说明

◆ TTripleBuffer() [1/2]

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

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

参数
InValueThe initial value of all three buffers.

◆ TTripleBuffer() [2/2]

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

参数
InBuffersThe buffer memory to use.

成员函数说明

◆ GetWriteBuffer()

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

Get the current write buffer.

返回
Reference to write buffer.

◆ IsDirty()

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

Check whether a new value is available for reading.

返回
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.

返回
Reference to the read buffer's value.
参见
SwapRead, Write

◆ SwapAndRead()

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

Convenience method for fetching and reading the latest buffer.

返回
Reference to the buffer.
参见
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).

参见
SwapWrite, Read

◆ SwapWriteBuffers()

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

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

参见
SwapRead, Write

◆ Write()

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

Write a value to the current write buffer.

参数
ValueThe value to write.
参见
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.

参数
ValueThe value to write into the buffer.
参见
SwapAndRead, SwapWrite, Write

该类的文档由以下文件生成: