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.

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.

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.
False if thread local pointer cannot be created.

◆ Get()

void* Get ( ) const

Returns a thread local pointer value.

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

◆ Set()

Bool Set ( void *  value)

Sets a thread local pointer value.

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

◆ Reset()

Bool Reset ( )

Resets a thread local pointer to the uninitialized state.

False if previously uninitialized.

◆ IsValid()

Bool IsValid ( ) const

For debugging purposes.

True if initialized.

Member Data Documentation


const pthread_key_t INVALID_KEY

◆ _key

pthread_key_t _key