documentation boost::thread_specific_ptr
状态(强调我的):
Note: on some platforms, cleanup of thread-specific data is not performed for threads created with the platform's native API. On those platforms such cleanup is only done for threads that are started with boost::thread unless boost::on_thread_exit() is called manually from that thread.
这些不执行清理的平台是什么? (动机:我想用 C++11 之前的编译器模拟 thread_local
并且调用指向对象的析构函数是至关重要的)。
最佳答案
POSIX 线程(pthreads)提供了清理线程本地存储的接口(interface),因此此言论不涉及任何正确支持 pthreads 的平台。
在 Windows 上,没有用于 TLS 清理的 native API,因此库必须求助于各种 hack 来实现它。从源代码中(有关 Boost.Thread 构建为 dll 的情况,请参阅 here,当它是静态库时请参阅 here),您可以看到支持 MSVC 和 MinGW/MinGW-w64。 dll 版本是相当可移植的,因此如果您在 Windows 上使用一些奇特的编译器并且 Boost.Thread 是作为静态库构建的,那么清理实现可能会丢失。
Boost.Thread 为需要用户提供 TLS 清理实现的情况提供了一种指示机制。由于缺少函数 boost::tss_cleanup_implemented
,应用程序不会链接。当出现这样的错误时,用户应该实现 TLS 清理和这个功能(空实现就足够了)。当清理由 Boost.Thread 实现时,该函数也由 Boost.Thread 定义。
关于c++ - 在没有 boost::thread 的情况下,thread_specific_pointer 在哪些平台上工作?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42855278/