5

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).

Community
  • 1
  • 1
slyqualin
  • 297
  • 2
  • 12

2 Answers2

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.

slyqualin
  • 297
  • 2
  • 12
0

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.

Maxim Egorushkin
  • 131,725
  • 17
  • 180
  • 271
  • Sorry if I'm being slow, but I don't yet see what I need to do. My code is (I hope) going to run on various systems, some posix some not, and I hoped that boost would allow me to write something that would not depend on that. Is this hope realistic? The quote from the boost doco seems to suggest that I can solve it at the level of boost API calls. – slyqualin Mar 17 '14 at 09:48
  • @slyqualin You don't need to do anything special if you use `boost::thread` for spawning new threads. I suggest taking some time to read the documentation. – Maxim Egorushkin Mar 17 '14 at 09:58
  • I don't know what mechanism my users are going to use, to spawn their threads. That's why I am interested in how boost::thread_specific_ptr behaves with _threads created with the platform's native API_ (i.e. any sort of threads). Unfortunately the only documentation I have found that is relevant to that question is the enigmatic paragraph that I quoted at the beginning. – slyqualin Mar 17 '14 at 10:10