线程本地存储Thread Local Storage

Microsoft 专用Microsoft Specific

线程本地存储 (TLS) 是给定的多线程进程中的每个线程为线程特定的数据分配存储时所采用的机制。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. 有关线程的完整讨论,请参阅 --- --- Windows SDKWindows SDK 中的进程和线程For a complete discussion of threads, see Processes and Threads in the --- --- Windows SDKWindows SDK.

Microsoft C 语言包括扩展的存储类特性 thread,可将它与 __declspec 关键字一起使用来声明线程本地变量。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:

  • __declspec(thread) 的使用可能会影响 DLL 导入的延迟加载The use of __declspec(thread) may interfere with delay loading of DLL imports.

  • 您只能将 thread 特性应用于数据声明和定义。You can apply the thread attribute only to data declarations and definitions. 它不能用于函数声明或定义。It cannot be used on function declarations or definitions. 例如,下面的代码生成一个编译器错误:For example, the following code generates a compiler error:

    #define Thread   __declspec( thread )  
    Thread void func();      /* Error */  
    
  • 只能在具有静态存储持续时间的数据项上指定 thread 特性。You can specify the thread attribute only on data items with static storage duration. 这包括全局数据(静态的和外部的)和本地静态数据。This includes global data (both static and extern) and local static data. 不能使用 thread 特性声明自动数据。You cannot declare automatic data with the thread attribute. 例如,下面的代码将生成编译器错误:For example, the following code generates compiler errors:

    #define Thread   __declspec( thread )  
    void func1()  
    {  
        Thread int tls_i;            /* Error */  
    }  
    
    int func2( Thread int tls_i )    /* Error */  
    {  
       return tls_i;  
    }  
    
  • 必须将 thread 特性用于线程本地数据的声明和定义,无论声明和定义是出现在同一文件中还是单独的文件中。You must use the thread attribute for the declaration and the definition of thread local data, regardless of whether the declaration and definition occur in the same file or separate files. 例如,下面的代码将生成错误:For example, the following code generates an error:

    #define Thread   __declspec( thread )  
    extern int tls_i;     /* This generates an error, because the   */  
    int Thread tls_i;     /* declaration and the definition differ. */  
    
  • 无法将 thread 特性用作类型修饰符。You cannot use the thread attribute as a type modifier. 例如,下面的代码生成一个编译器错误:For example, the following code generates a compiler error:

    char *ch __declspec( thread );      /* Error */  
    
  • 不将线程局部变量的地址视为常数,并且涉及此类地址的任何表达式不会被视为常量表达式。The address of a thread local variable is not considered constant, and any expression involving such an address is not considered a constant expression. 这意味着,无法将线程局部变量的地址用作指针的初始值设定项。This means that you cannot use the address of a thread local variable as an initializer for a pointer. 例如,编译器会将下面的代码标记为错误:For example, the compiler flags the following code as an error:

    #define Thread   __declspec( thread )  
    Thread int tls_i;  
    int *p = &tls_i;      /* Error */  
    
  • C 允许使用涉及对自身的引用的表达式来初始化变量,但只适用于非静态范围的对象。C permits initialization of a variable with an expression involving a reference to itself, but only for objects of nonstatic extent. 例如:For example:

    #define Thread   __declspec( thread )  
    Thread int tls_i = tls_i;             /* Error */  
    int j = j;                            /* Error */  
    Thread int tls_i = sizeof( tls_i )    /* Okay  */  
    

    请注意,包含正在初始化的变量的 sizeof 表达式不构成对自身的引用,并且允许使用该表达式。Note that a sizeof expression that includes the variable being initialized does not constitute a reference to itself and is allowed.

  • __declspec(thread) 的使用可能会影响 DLL 导入的延迟加载The use of __declspec(thread) may interfere with delay loading of DLL imports.

    有关使用 thread 特性的详细信息,请参阅多线程主题For more information about using the thread attribute, see Multithreading Topics.

    结束 Microsoft 专用END Microsoft Specific

另请参阅See Also

C 扩展的存储类特性C Extended Storage-Class Attributes