GemaCoreLib
The GeMA Core library
Public Types | Public Member Functions | Private Attributes | List of all members
GmTLBuffer< T, Align > Class Template Reference

A class similar to GmTLS that creates a buffer for each possible thread in the GmThreadManager, plus an aditional one for the main thread. More...

#include <gmThreadLocalBuffer.h>

Collaboration diagram for GmTLBuffer< T, Align >:
Collaboration graph
[legend]

Public Types

typedef GmTLBuffer_Alloc< T, Align > Alloc
 

Public Member Functions

 GmTLBuffer ()
 Default constructor. Buffer sizes MUST be initialized by a call to setSize()
 
 GmTLBuffer (int n)
 Constructor. Each thread buffer will store n entries of type T.
 
 GmTLBuffer (int n, const T &defVal)
 Constructor. Each thread buffer will store n entries of type T, initialized to defVal.
 
 ~GmTLBuffer ()
 Destructor.
 
void clear ()
 Clears the allocated buffers returning the object to a default constructed state.
 
int size () const
 Returns the buffer size.
 
void setSize (int n)
 Set buffer size for a default constructed buffer.
 
void setSize (int n, const T &defVal)
 Set buffer size for a default constructed buffer and initializes the contents to defVal.
 
void resize (int n)
 Change buffer size.
 
T * localBuffer (int tid)
 Returns a pointer to the given thread local buffer.
 
const T * localBuffer (int tid) const
 Returns a const pointer to the given thread local buffer.
 
T * localBuffer ()
 Returns a pointer to the current thread local buffer.
 
const T * localBuffer () const
 Returns a const pointer to the current thread local buffer.
 
void init (const T &defVal)
 Initializes each buffer entry to defVal.
 

Private Attributes

GmTLS< T * > _data
 The local storage with pointers to each thread buffer. No need to align _data since worker threads will only read the buffer pointers and not change them (they will change the buffer contents, not the buffer locations)
 
int _n
 The size of each buffer.
 

Detailed Description

template<class T, bool Align = false>
class GmTLBuffer< T, Align >

A class similar to GmTLS that creates a buffer for each possible thread in the GmThreadManager, plus an aditional one for the main thread.

The decalaration of a GmTLBuffer<double> variable creates a thread local storage that stores one double buffer for each thread. The buffer size is given in the constructor.

If Align is set to true, the class ensures that each buffer is cache aligned.

IMPORTANT: Every GmTLBuffer object MUST be destructed BEFORE the thread manager.

COROLLARY: As a consequence, one should never create a TLS object as a global variable since its life time will surelly begin before the thread manager is created and end after its destruction.


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