Thread Local Storage

Thread Local Storage (TLS) calls allow you to store per-thread data. TLS is the method by which each thread in a multithreaded process can allocate locations in which to store thread-specific data.

Dynamically bound (run-time) thread-specific data is supported by routines such as TlsAlloc (allocates an index to store data), TlsGetValue (retrieves values from an index), TlsSetValue (stores values into an index), and TlsFree (frees the dynamic storage). Threads allocate dynamic storage and use TlsSetValue to associate the index with a pointer to that storage. When a thread needs to access the storage, it calls TlsGetValue, specifying the index.

When all threads have finished using the index, TlsFree frees the dynamic storage.