cisst-saw
|
#include <osaTripleBuffer.h>
Public Member Functions | |
osaTripleBuffer (void) | |
osaTripleBuffer (const_reference initialValue) | |
osaTripleBuffer (pointer pointer1, pointer pointer2, pointer pointer3) | |
void | SetupNodes (pointer pointer1, pointer pointer2, pointer pointer3) |
~osaTripleBuffer () | |
void | Read (reference placeHolder) |
void | Write (const_reference newValue) |
const_pointer | GetReadPointer (void) const |
pointer | GetWritePointer (void) const |
void | BeginRead (void) |
void | EndRead (void) |
void | BeginWrite (void) |
void | EndWrite (void) |
void | ToStream (std::ostream &outputStream) const |
Friends | |
class | osaTripleBufferTest |
Triple buffer to implement a thread safe, lock free, single reader single writer container. This relies on a LIFO circular buffer with only three slots, one to read the latest value and two to write (assuming the reading thread can be slow to read).
This class assumes read and write operations are performed in two different threads. The reader must trigger the following calls to be safe: BeginRead, GetReadPointer and finally EndRead. Similarly, the writer must call BeginWrite, GetWritePointer and EndWrite. It is important to not cache the results of both GetReadPointer and GetWritePointer.
The triple buffer can be constructed using three existing pointers on valid memory slots or allocate the memory itself (see constructors).
The implementation relies on a mutex (using osaMutex) to make sure the BeginRead, EndRead, BeginWrite and EndWrite methods are thread safe. The mutex is used for very brief operations so there shouldn't be any long wait times.
|
inline |
Constructor that allocates memory for the triple buffer using the default constructor for each element.
|
inline |
Constructor that allocates memory for the triple buffer using the copy constructor for each element. User has to provide an value which will be used to initialize the buffer elements.
|
inline |
Constructor that doesn't allocate any memory, user has to provide 3 valid pointers on 3 different pre-allocated objects. This can be used in combination with libraries such as VTK which have private constructors.
|
inline |
Destructor. If the memory is owned, it will delete the 3 objects allocated. All nodes get deleted.
|
inline |
Method used to find and lock the read node in the triple buffer. To access the actual memory, use GetReadPointer.
|
inline |
Method used to find and lock the write node in the triple buffer. To access the actual memory, use GetReadPointer.
|
inline |
Method to unlock the read node.
|
inline |
Method to unlock the write node.
|
inline |
Function to access the memory to read safely. This method call must be preceeded by a call to BeginRead and followed by a call to EndRead. All three calls must be performed in the same thread space.
|
inline |
Function to access the memory to write safely. This method call must be preceeded by a call to BeginWrite and followed by a call to EndWrite. All three calls must be performed in the same thread space.
|
inline |
Calls BeginRead, assign the last written value using the operator = and then calls EndRead.
|
inline |
Internal method to setup all nodes of the circular buffer. It requires 3 valid pointers.
|
inline |
Method to display current state of triple buffer
|
inline |
Calls BeginWrite, assign the new value to the current write location using the operator = and then calls EndWrite.
|
friend |