Search code examples
c++multithreadingboostthread-safetythread-local-storage

How do thread specific pointers work


Recently I have discovered the magic of boost thread specific pointers. After some research on thread safety I would like to know how exactly boost thread specific pointers work.However, I can't seem to find this in the documentation. Can anyone provide some insight on this or some detailed documentation?


Solution

  • From the Boost docs on thread local storage:

    boost::thread_specific_ptr provides a portable mechanism for thread-local storage that works on all compilers supported by Boost.Thread. Each instance of boost::thread_specific_ptr represents a pointer to an object (such as errno) where each thread must have a distinct value. The value for the current thread can be obtained using the get() member function, or by using the * and -> pointer deference operators. Initially the pointer has a value of NULL in each thread, but the value for the current thread can be set using the reset() member function.

    And you can find the Boost source here.