问题
I'm reading the documentation section for boost::thread_specific_ptr, and trying to parse this paragraph:
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.
First, what is probably a pedantic point: I assume they meant to say boost::this_thread::at_thread_exit()
rather than boost::on_thread_exit()
. Otherwise I really am lost.
More importantly, what exactly does the thread need to do? Is it sufficient for it to pass some no-op function to at_thread_exit(), or does it need to pass something else?
(This topic was discussed in comments here, but I'm still not sure what I need to do.)
(Back story: I'm looking for a solution to the problem I raised earlier today).
回答1:
After some more digging, it seems that the enigmatic paragraph did indeed mean to say on_thread_exit()
. It was referring to an undocumented function, which takes no arguments.
Here is that function's declaration and the accompanying comment, from 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.
So, iiuc, what I need to do is write platform-specific code that will trigger a call to this function whenever a thread of any kind terminates, if that thread has been using boost::thread_specific_ptr
.
回答2:
When a thread exits it needs to destroy its values of thread-specific pointers. On POSIX systems this is done by the destructor function registered when the thread-specific key is created, see http://pubs.opengroup.org/onlinepubs/007904975/functions/pthread_key_create.html, i.e. that comment does not apply to POSIX systems.
That comment is probably about Windows, where one is also required to link against the shared library version of boost::thread
, otherwise thread-specific pointers don't get destroyed.
来源:https://stackoverflow.com/questions/22448022/using-boostthread-specific-ptr-in-a-non-boost-thread