cisst-saw
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Classes | Public Member Functions | Friends | List of all members
osaTripleBuffer< _elementType > Class Template Reference

#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
 

Detailed Description

template<class _elementType>
class osaTripleBuffer< _elementType >

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.

Constructor & Destructor Documentation

template<class _elementType >
osaTripleBuffer< _elementType >::osaTripleBuffer ( void  )
inline

Constructor that allocates memory for the triple buffer using the default constructor for each element.

template<class _elementType >
osaTripleBuffer< _elementType >::osaTripleBuffer ( const_reference  initialValue)
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.

template<class _elementType >
osaTripleBuffer< _elementType >::osaTripleBuffer ( pointer  pointer1,
pointer  pointer2,
pointer  pointer3 
)
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.

template<class _elementType >
osaTripleBuffer< _elementType >::~osaTripleBuffer ( )
inline

Destructor. If the memory is owned, it will delete the 3 objects allocated. All nodes get deleted.

Member Function Documentation

template<class _elementType >
void osaTripleBuffer< _elementType >::BeginRead ( void  )
inline

Method used to find and lock the read node in the triple buffer. To access the actual memory, use GetReadPointer.

template<class _elementType >
void osaTripleBuffer< _elementType >::BeginWrite ( void  )
inline

Method used to find and lock the write node in the triple buffer. To access the actual memory, use GetReadPointer.

template<class _elementType >
void osaTripleBuffer< _elementType >::EndRead ( void  )
inline

Method to unlock the read node.

template<class _elementType >
void osaTripleBuffer< _elementType >::EndWrite ( void  )
inline

Method to unlock the write node.

template<class _elementType >
const_pointer osaTripleBuffer< _elementType >::GetReadPointer ( void  ) const
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.

template<class _elementType >
pointer osaTripleBuffer< _elementType >::GetWritePointer ( void  ) const
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.

template<class _elementType >
void osaTripleBuffer< _elementType >::Read ( reference  placeHolder)
inline

Calls BeginRead, assign the last written value using the operator = and then calls EndRead.

template<class _elementType >
void osaTripleBuffer< _elementType >::SetupNodes ( pointer  pointer1,
pointer  pointer2,
pointer  pointer3 
)
inline

Internal method to setup all nodes of the circular buffer. It requires 3 valid pointers.

template<class _elementType >
void osaTripleBuffer< _elementType >::ToStream ( std::ostream &  outputStream) const
inline

Method to display current state of triple buffer

template<class _elementType >
void osaTripleBuffer< _elementType >::Write ( const_reference  newValue)
inline

Calls BeginWrite, assign the new value to the current write location using the operator = and then calls EndWrite.

Friends And Related Function Documentation

template<class _elementType >
friend class osaTripleBufferTest
friend

The documentation for this class was generated from the following file: