Microsoft Specific —>
Thread Local Storage (TLS) is the mechanism by which each thread in a given multithreaded process allocates storage for thread-specific data. In standard multithreaded programs, data is shared among all threads of a given process, whereas thread local storage is the mechanism for allocating per-thread data. For a complete discussion of threads, see Processes and Threads in the Microsoft Win32® Software Development Kit documentation.
The Microsoft C language includes the extended storage-class attribute, thread, which is used with the __declspec keyword to declare a thread local variable. For example, the following code declares an integer thread local variable and initializes it with a value:
__declspec( thread ) int tls_i = 1;
These guidelines must be observed when you are declaring statically bound thread local variables:
#define Thread __declspec( thread )
Thread void func(); /* Error */
#define Thread __declspec( thread )
void func1()
{
Thread int tls_i; /* Error */
}
int func2( Thread int tls_i ) /* Error */
{
return tls_i;
}
#define Thread __declspec( thread )
extern int tls_i; /* This generates an error, because the */
int Thread tls_i; /* declaration and the definition differ. */
char *ch __declspec( thread ); /* Error */
#define Thread __declspec( thread )
Thread int tls_i;
int *p = &tls_i; /* Error */
#define Thread __declspec( thread )
Thread int tls_i = tls_i; /* Error */
int j = j; /* Error */
Thread int tls_i = sizeof( tls_i ) /* Okay */
Note that a sizeof expression that includes the variable being initialized does not constitute a reference to itself and is allowed.
For more information about using the thread attribute, see Multithreading Topics in Visual C++ Programmer’s Guide.
END Microsoft Specific