ThreadLocalPointer Class Reference

#include <threadlocalpointer.h>

Inheritance diagram for ThreadLocalPointer:

Detailed Description

Thread local pointer (to a data structure). Different threads may use this class to store different (local) values to it. These values are typically pointers to data which is to be used by the calling thread only.

Note
Thread local pointers are a scarce resource. Only a limited number of them can be allocated: On Windows about 1000 and on OS X about 500. You should only use them if they are the last and only resort to solve a problem. Furthemore for performance reasons it is best to avoid thread local pointers at all and instead use some sort of local context (for a group's jobs or with ParallelFor).
A thread local pointer is bound to a OS thread and the pointer is stored for the lifetime of the thread.

Public Member Functions

 ThreadLocalPointer ()
 
 ~ThreadLocalPointer ()
 
Bool Init ()
 
void * Get () const
 
Bool Set (void *value)
 
Bool Reset ()
 
Bool IsValid () const
 

Protected Attributes

pthread_key_t _key
 

Static Private Attributes

static const pthread_key_t INVALID_KEY
 

Constructor & Destructor Documentation

◆ ThreadLocalPointer()

◆ ~ThreadLocalPointer()

Member Function Documentation

◆ Init()

Bool Init ( )

Allocates and initializes a thread local pointer.

Note
If you Init() an already initialized thread local pointer, a new one will be created and you cannot access the old one anymore. This behaviour is intended.
Returns
False if thread local pointer cannot be created.

◆ Get()

void* Get ( ) const

Returns a thread local pointer value.

Note
If no value has been set yet, this function returns nullptr. THREADSAFE.
Returns
Thread local pointer value.

◆ Set()

Bool Set ( void *  value)

Sets a thread local pointer value.

Parameters
[in]valueThread local pointer value. THREADSAFE.
Returns
True unless an initial memory allocation failed or the initialization was missing.

◆ Reset()

Bool Reset ( )

Resets a thread local pointer to the uninitialized state.

Returns
False if previously uninitialized.

◆ IsValid()

Bool IsValid ( ) const

For debugging purposes.

Returns
True if initialized.

Member Data Documentation

◆ INVALID_KEY

const pthread_key_t INVALID_KEY
staticprivate

◆ _key

pthread_key_t _key
protected