Thread-local storage
Thread-local Storage (TLS) is a memory management technique that allows each thread to have its own instance of a variable. This technique allows threads to store thread-specific data that is not accessible by other threads, avoiding race conditions and improving performance. This is because the overhead of synchronization mechanisms to access these variables is removed.
TLS is implemented by the OS and accessible by using the thread_local
keyword, which has been available since C++11. thread_local
provides a uniform way to use the TLS capabilities of many OSs and avoid compiler-specific language extensions for accessing the TLS feature (some examples of such extensions are the TLS Windows API, the __declspec(thread)
MSVC compiler language extension, or the __thread
GCC compiler language extension).
To use TLS with compilers that do not support C++11 or newer versions, use Boost.Library
. This provides the boost::thread_specific_ptr
container, which implements...