Thread-Local Storage (TLS) allows an application or DLL to store data on behalf of a thread. Functions exist to allocate and free a TLS index, and to locate a TLS slot using its index.
Additional support provided by Win32 includes calling DLLs each time a thread begins execution, and each time a thread exits. This gives a DLL an opportunity to initialize and free its thread local storage slots.
The TLS functions operate on the calling thread/processes TLS data structures. Each process contains a minimum number of guaranteed thread local storage indexes. Once an index is allocated, it can be used to address a TLS slot in each thread.
Each process contains a bitmap of TLS indexes. The bitmap has a fixed minimum size. The minimum size is 64 (TLS_MINIMUM_AVAILABLE).
Each thread has an array of pointers. The array is indexed by a TLS index.
Indexes are allocated by an application or DLL. An index is valid within the calling process. Indexes are typically allocated during application initialization or during DLL initialization. Once allocated, an application or DLL may use the TLS index to locate a per thread TLS slot. TLS indexes are not valid across process boundaries. A DLL can not assume that an index assigned in one process is valid in another process.
These Thread-Local Storage include:
Function | Description |
TlsAlloc | Allocates thread-local storage |
TlsFree | Frees thread-local storage |
TlsGetValue | Gets a thread-local value. |
TlsSetValue | Sets a thread-local value. |