Thread Local Storage. More...
#include <rthread.h>
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
-
size Initial size of the internal containers. It is set to 20 by default.
Member Function Documentation
|
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
-
id Unique 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
-
id Identifier that has reserved the data structure. null Return 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
-
id Identifier that has reserved the data structure. null Return a null pointer or generate an exception ?
- Returns
- a pointer to a data structure.
void Release | ( | size_t | id | ) |
Release a data structure.
- Parameters
-
id Identifier that has reserved the data structure.
Field Documentation
|
private |
Container of data structures.
|
private |
Vector that remember which data structures are actually used.
|
private |
Relation between an identifier and the index of the corresponding data structure used.
|
private |
Read/Write lock.