ThreadLocalPointer< P > Class Template Reference

#include <threadlocalpointer.h>

Inheritance diagram for ThreadLocalPointer< P >:

Detailed Description

template<typename P>
class maxon::ThreadLocalPointer< P >

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 ()
 
Bool Init ()
 
P * Get () const
 
 operator P* () const
 
Bool Set (P *value)
 
ThreadLocalPointeroperator= (P *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()

Member Function Documentation

◆ Init()

Bool Init ( void  )

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()

P* Get ( void  ) 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.

◆ operator P*()

operator P* ( ) const

◆ Set()

Bool Set ( P *  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.

◆ operator=()

ThreadLocalPointer& operator= ( P *  value)

◆ Reset()

Bool Reset ( void  )

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