c++ - 在非 boost 线程中使用 boost::thread_specific_ptr

标签 c++ multithreading boost boost-thread thread-local-storage

我正在阅读 the documentation section for 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.

首先,可能是迂腐的一点:我假设他们的意思是说 boost::this_thread::at_thread_exit() 而不是 boost::on_thread_exit()。否则我真的迷路了。

更重要的是,线程到底需要做什么?将一些空操作函数传递给 at_thread_exit() 就足够了吗,还是需要传递其他东西?

(这个主题在评论 here 中讨论过,但我仍然不确定我需要做什么。)

(背景故事:我正在寻找我提出的问题的解决方案 earlier today)。

最佳答案

经过更多的挖掘,似乎那个神秘的段落确实意味着说 on_thread_exit()。它指的是一个未记录的函数,它不带任何参数。

这是该函数的声明和随附的注释,来自 boost_1_55_0/boost/thread/detail/tss_hooks.hpp:

BOOST_THREAD_DECL void __cdecl on_thread_exit(void);
    //Function to be called just be fore a thread ends
        //in an exe or dll that uses Boost.Threads.
    //Must be called in the context of the thread
        //that is ending.
    //Called automatically by Boost.Threads when
        //a method for doing so has been discovered.
    //Must not be omitted; may be called multiple times.

所以,iiuc,我需要做的是编写特定于平台的代码,只要任何类型的线程终止,如果该线程一直在使用 boost::thread_specific_ptr,就会触发对该函数的调用>.

关于c++ - 在非 boost 线程中使用 boost::thread_specific_ptr,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22448022/

相关文章:

c++ - 从机器名称中查找机器 IP 地址

c++ - ACE(C++ 库)是否仍用于高性能计算?

c# - 将 C++ 结构转换为 C# 以用于 UDP 通信

c++ - 无法使用 Microsoft Visual Studio C++ 2008 速成版编译 C 代码

string - 我如何在 Rust (crossbeam) 中与多个生产者和一个接收者共享字符串消息?

ios - 使用 NSThread 生成线程时如何解决 "target does not implement selector"这个异常?

c# 从另一个线程而不是 UI 线程调用 backgroundWorker

c++ - 在 CMake 中仅指定某些源文件

c++ - 分配器专门用于 c++14 中的数组类型?

c++ - libicuuc.so.52,需要 libboost_regex-mt.so