I need to make use of thread-local storage in a cross-platform project. Under *IX I am using pthreads and can avoid memory leaks thanks to the nice destructor function pointer passed as the second argument to pthread_key_create
, but in Windows TlsAlloc
has no such thing. Nor can I find a general place where any function is called on thread exit (otherwise I would homebrew some list of function pointers that get called at exit).
As it stands it seems that I have basically a situation where in order to actually use thread local storage I need to allocate my own space on the heap and pass a pointer to TlsSetValue
, but if the thread exits ... I have no way of ensuring the memory was freed (other than someone explicitly calling TlsGetValue
and delete
/free
/HeapFree
/etc at the end of the thread function.
Does anyone know of a better way?