How to cleanup TSD in Win32?
>I am forced to use TSD in multithreading existing code. I just ran >into the problem that, while the destructor function argument to >pthread_key_create() and thr_keycreate() appears ideal, there is no >such facility with the NT TlsAlloc() and that related stuff. It’s pretty easy to write the code to provide this facility. Basically you have to wrap TlsAlloc() and TlsFree() in some additional logic. This logic maintains a list of all the keys currently allocated for your process. For each allocated key it stores the address of the destructor routine for that key (supplied as an argument to the wrapped version of TlsAlloc()). When a thread exits it iterates through these records; for every key that references valid data (i.e. TlsGetValue() returns non-NULL) call the relevant destructor routine, supplying the address of the thread specific data as an argument. The tricky part to all this is figuring out when to call the routine that invokes the destructors. If you have complete control