简体   繁体   English

无论如何在Win32 API中动态释放线程本地存储?

[英]Is there anyway to dynamically free thread-local storage in the Win32 APIs?

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. 在* IX下我使用pthreads并且可以避免内存泄漏,这要归功于作为pthread_key_create的第二个参数传递的漂亮的析构函数指针,但在Windows中TlsAlloc没有这样的东西。 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. 现在看起来我基本上有一种情况,为了实际使用线程本地存储我需要在堆上分配我自己的空间并传递指向TlsSetValue ,但如果线程退出...我无法确保释放内存(除了在线程函数结束时显式调用TlsGetValuedelete / free / HeapFree / etc的人。

Does anyone know of a better way? 有谁知道更好的方法?

You can get yourself a nice "finalizer" to get rid of thread-specific resources, even if the thread is terminated: use RegisterWaitForSingleObject to wait on a copy (via DuplicateHandle ) of the threads' handle - you have to use the cloned handle, cause registered waits can't handle handle {no pun intended} closing. 即使线程被终止,你也可以找到一个很好的“终结器”来摆脱特定于线程的资源:使用RegisterWaitForSingleObject等待线程句柄的副本 (通过DuplicateHandle ) - 你必须使用克隆的句柄,因为注册等待无法处理句柄{没有双关语意图}关闭。
Use a heap allocated structure/record to hold the finalized resources, the handle waited for and the wait handle itself, cause the finalizer will run in the system threadpool, NOT the finalized thread (which will be already dead by the time). 使用堆分配的结构/记录来保存最终的资源,句柄等待和等待句柄本身,导致终结器将在系统线程池中运行,而不是最终的线程 (它将在时间已经死亡)。 And don't forget to finalize the finalizer :) 并且不要忘记敲定终结者:)

The entry point of a DLL ( DLLmain ) is called on thread exit with a reason code of DLL_THREAD_DETACH . 在线程出口上调用DLL( DLLmain )的入口点,原因码为DLL_THREAD_DETACH It is fairly straightforward to write a DLL that keeps track of functions to call on thread exit. 编写一个DLL来跟踪在线程退出时调用的函数是相当简单的。

Alternatively, use Boost.Thread and the boost::this_thread::at_thread_exit function to register a function to be called on thread exit, or use boost::thread_specific_ptr to wrap the TLS usage entirely. 或者,使用Boost.Threadboost::this_thread::at_thread_exit函数来注册要在线程退出时调用的函数,或者使用boost::thread_specific_ptr来完全包装TLS用法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM