Thread Local Storage. More...

#include <rthread.h>

Collaboration diagram for RThreadLocalStorage< C >:
[legend]

Public Member Functions

 RThreadLocalStorage (size_t size=20)
 
virtual C * New (void)
 
C * Reserve (size_t id)
 
C * Get (size_t id, bool null=false)
 
const C * Get (size_t id, bool null=false) const
 
void Release (size_t id)
 

Private Attributes

RContainer< C, true, false > Data
 
RBoolVector DataUsed
 
RNumContainer< size_t, false > Index
 
RRWLock lData
 

Detailed Description

template<class C>
class R::RThreadLocalStorage< C >

Thread Local Storage.

The RThreadLocalStorage class provides local storage for threads. It is sometimes necessary that each thread works on its own copy of a data structure. Moreover, this data structure could be persistent, i.e. once a thread uses the data structure, non other one should use it.

When a thread needs a structure, it calls the RThreadLocalStorage::Reserve method and passes an unique identifier that can be its own identifier (remember that it is the responsibility of the developer to ensure the unicity of the thread identifiers). A call to RThreadLocalStorage::Release makes the data structure free again to be used by another thread. The persistent of a data structure can be obtained by never calling the RThreadLocalStorage::Release method.

The RThreadLocalStorage class ensures that only the necessary amount of data structures is allocated on the heap.

Constructor & Destructor Documentation

RThreadLocalStorage ( size_t  size = 20)

Constructor.

Parameters
sizeInitial size of the internal containers. It is set to 20 by default.

Member Function Documentation

virtual C* New ( void  )
virtual

Method that allocates a new data structure. This method can overloaded to eventually call more complex constructors.

Returns
a pointer to a new allocated data structure.
C* Reserve ( size_t  id)

Reserve a data structure.

Parameters
idUnique identifier that will be associated to the data structure.
Returns
a pointer to a data structure.
C* Get ( size_t  id,
bool  null = false 
)

Get a data structure. If no data structure is reserved, an exception is generated or a null pointer is returned.

Parameters
idIdentifier that has reserved the data structure.
nullReturn a null pointer or generate an exception ?
Returns
a pointer to a data structure.
const C* Get ( size_t  id,
bool  null = false 
) const

Get a data structure (const version). If no data structure is reserved, an exception is generated or a null pointer is returned.

Parameters
idIdentifier that has reserved the data structure.
nullReturn a null pointer or generate an exception ?
Returns
a pointer to a data structure.
void Release ( size_t  id)

Release a data structure.

Parameters
idIdentifier that has reserved the data structure.

Field Documentation

RContainer<C,true,false> Data
private

Container of data structures.

RBoolVector DataUsed
private

Vector that remember which data structures are actually used.

RNumContainer<size_t,false> Index
private

Relation between an identifier and the index of the corresponding data structure used.

RRWLock lData
private

Read/Write lock.