### Thread Local Storage (Windows)

Thread Local Storage (TLS) is a mechanism whereby each thread in a multithread process will allocate storage for the data corresponding to that thread. IBM C and C++ Compilers provides both a dynamic and a static technique of using TLS.

Dynamic TLS is provided by a set of four Win32 API's: TlsAlloc, TlsFree, TlsSetValue, and TlsGetValue. These functions are more difficult to use, because you must handle the allocation and initialization of the thread-local data. However, there are situations in which you have to use them.

Static TLS uses the same concept as dynamic TLS but has the advantage of being simpler from the high-level view. It allows TLS data to be defined and initialized in a manner similar to ordinary static variables.

The thread local data objects are declared by the __thread attribute:

      __thread dataObject

where dataObject can be one of the following:

- Global data objects
- Local static data objects
- Static data members of a class

Multithreaded Applications

Rules and Restrictions on Using TLS