6.6. Using Thread Local Storage

The 32-bit APIs contains a unique feature called Thread Local Storage (TLS), which you can use in processes and DLLs in a variety of ways. The capability itself is very straightforward, but its extended ramifications are what make it interesting.

The TLS capability consists of four very simple functions: TlsAlloc, TlsFree, TlsSetValue, and TlsGetValue. You use TLS by first calling the TlsAlloc function, as shown below:

DWORD tlsIndex;   // probably a global variable

...

// allocate a thread local storage index
tlsIndex=TlsAlloc();

The call to TlsAlloc creates a block of memory that contains one 32-bit storage location for each existing thread in a process. Any new threads created after the call to TlsAlloc ...

Get Win32 System Services: The Heart of Windows® 98 and Windows® 2000 now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.