In any case, is it possible to dynamically free up streaming local storage in the Win32 API?

I need to use thread-local storage in a cross-platform project. In section * IX, I use pthreads and can avoid memory leaks thanks to a good pointer to the destructor function passed as the second argument pthread_key_create, but on Windows TlsAllocthere is no such thing. I also cannot find a common place where any function is called when the stream exits (otherwise, I would run a list of function pointers that are called when the stream exits).

Be that as it may, it seems that I basically have a situation where for the actual use of streaming local storage I need to allocate my own space on the heap and pass a pointer to it TlsSetValue, but if the stream exits ... I have no way to ensure memory is freed (except those who explicitly call TlsGetValueand delete/ free/ HeapFree/ etc at the end of the stream function.

Does anyone know a better way?

+3
source share
2 answers

The entry point to the DLL ( DLLmain) is called when a stream exits with a reason code DLL_THREAD_DETACH. Just write a DLL that tracks the functions to call the output of the stream.

Boost.Thread boost::this_thread::at_thread_exit , , boost::thread_specific_ptr TLS.

+3

"", , , : RegisterWaitForSingleObject ( DuplicateHandle) - , {no pun designed} .
/, , , , ( ). :)

+3

Source: https://habr.com/ru/post/1754506/


All Articles