Search code examples
c++multithreadingboostboost-threadthread-local-storage

Using boost::thread_specific_ptr in a non-boost thread


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


Solution

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