ThreadLocalDestructiblePointer Class Reference

#include <threadlocalpointer.h>

Inheritance diagram for ThreadLocalDestructiblePointer:

Detailed Description

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.

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. Furthemore 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

 ThreadLocalDestructiblePointer ()
 ~ThreadLocalDestructiblePointer ()
Bool Init (void(*destructionNotification)(void *))
- Public Member Functions inherited from ThreadLocalPointer
 ThreadLocalPointer ()
 ~ThreadLocalPointer ()
Bool Init ()
void * Get () const
Bool Set (void *value)
Bool Reset ()
Bool IsValid () const

Private Member Functions

Bool Init ()

Additional Inherited Members

- Protected Attributes inherited from ThreadLocalPointer
pthread_key_t _key

Constructor & Destructor Documentation

◆ ThreadLocalDestructiblePointer()

◆ ~ThreadLocalDestructiblePointer()

Member Function Documentation

◆ Init() [1/2]

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

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

◆ Init() [2/2]

Bool Init ( )