Static vs. Dynamic TLS


The Win32 platform provides TLS in two different flavors: dynamic and static. The technique presented in this article uses dynamic TLS. Static TLS also associates data with a thread, but it is much easier to use. The compiler provides a modifier declspec(thread) that automatically allows you to associate the data with a TLS slot:

__declspec(thread) int n;

The above statement will create an instance of the integer n for every thread created by the process. Static TLS is not to be used by DLLs that are loaded in run time (e.g., with LoadLibrary) because the operating system does not handle that scenario (see [3] for details).