ThreadLocalDestructiblePointer< P > Class Template Reference

#include <threadlocalpointer.h>

Inheritance diagram for ThreadLocalDestructiblePointer< P >:

Detailed Description

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

Thread local pointer with notification upon destruction. This is similar to ThreadLocalPointer but when a thread exits or the object is destructed a per-thread notification is invoked which can be used to free resources.

Note
Thread local pointers are a scarce resource. Only a limited number of them can be allocated: On Windows about 100 (there are less FLS than TLS entries) and on OS X about 500. You should only use them if they are the last and only resort to solve a problem. Furthermore for performance reasons it is best to avoid thread local variables 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. If you create and use thread local variables in a module you must ensure that they are destructed properly before the module is unloaded.
On Windows this is 50 % slower than ThreadLocalPointer. Furthermore the code must not use the (deprecated) fiber calls (ConvertThreadToFiber, CreateFiber etc.).

Public Member Functions

Bool Init (void(*destructionNotification)(void *))
 
ThreadLocalDestructiblePointeroperator= (P *value)
 
- Public Member Functions inherited from ThreadLocalPointer< P >
 ~ThreadLocalPointer ()
 
Bool Init ()
 
P * Get () const
 
 operator P* () const
 
Bool Set (P *value)
 
ThreadLocalPointeroperator= (P *value)
 
Bool Reset ()
 
Bool IsValid () const
 

Private Member Functions

Bool Init ()
 

Additional Inherited Members

- Protected Attributes inherited from ThreadLocalPointer< P >
pthread_key_t _key
 

Member Function Documentation

◆ Init() [1/2]

Bool Init ( void(*)(void *)  destructionNotification)

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.
The notification will only be invoked if the pointer value is not a nullptr.
Parameters
[in]destructionNotificationTo be called when a thread exits or the thread local pointer is destructed.
Returns
False if thread local pointer cannot be created.

◆ operator=()

ThreadLocalDestructiblePointer& operator= ( P *  value)

◆ Init() [2/2]

Bool Init ( )
private