Static TLS

Like dynamic TLS, static TLS associates data with a thread. However, static TLS is much easier to use in your code because you don’t have to call any functions to take advantage of it.

Let’s say that you want to associate a start time with every thread created by your application. All you do is declare the start-time variable as follows:

__declspec(thread) DWORD gt_dwStartTime = 0;

The __declspec(thread) prefix is a modifier that Microsoft added to the Visual C++ compiler. It tells the compiler that the corresponding variable should be placed in its own section in the executable or DLL file. The variable following __declspec(thread) must be declared as either a global variable or a static variable inside (or outside) a function. You can’t ...

Get Windows® via C/C++, Fifth Edition 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.