线程局部存储Thread Local Storage(TLS)

来源:互联网 发布:java实现注册和登陆 编辑:程序博客网 时间:2024/05/16 01:32
 

在线程的学习中我们知道每个线程除了共享进程的资源外还拥有各自的私有资源:  

一个寄存器组(或者说是线程上下文);一个专属的堆栈;一个专属的消息队列;一个专属的Thread Local StorageTLS);一个专属的结构化异常处理串链。其中线程上下文在线程的学习中已经解释过了,堆栈没有什么好说的,消息队列会在USER GDI 子系统一节中讲解,那么这节的任务就是集中讲解线程局部存储Thread Local StorageTLS);结构化异常处理Structured Exception HandlingSEH)会在后面的单独讲解。 
 

TLS 是一个良好的Win32 特质,让多线程程序设计更容易一些。TLS 是一个机制,经由它,程序可以拥有全域变量,但处于「每一线程各不相同」的状态。也就是说,进程中的所有线程都可以拥有全域变量,但这些变量其实是特定对某个线程才有意义。例如,你可能有一个多线程程序,每一个线程都对不同的文件写文件(也因此它们使用不同的文件handle)。这种情况下,把每一个线程所使用的文件handle 储存在TLS 中,将会十分方便。当线程需要知道所使用的handle,它可以从TLS 获得。重点在于:线程用来取得文件handle 的那一段码在任何情况下都是相同的,而从TLS中取出的文件handle 却各不相同。非常灵巧,不是吗?有全域变数的便利,却又分属各线程。  
 

  虽然TLS 很方便,它并不是毫无限制。在Windows NT Windows 95 之中,有64 DWORD slots 供每一个线程使用。这意思是一个进程最多可以有64 个「对各线程有不同意义」的DWORDs 虽然TLS 可以存放单一数值如文件handle,更常的用途是放置指针,指向线程的私有资料。有许多情况,多线程程序需要储存一堆数据,而它们又都是与各线程相关。许多程序员对此的作法是把这些变量包装为C 结构,然后把结构指针储存在TLS 中。当新的线程诞生,程序就配置一些内存给该结构使用,并且把指针储存在为线程保留下来的TLS 中。一旦线程结束,程序代码就释放所有配置来的区块。既然每一个线程都有64 slots 用来储存线程自己的数据,那么这些空间到底打哪儿来?在线程的学习中我们可以从结构TDB中看到,每一个thread database 都有64 DWORDs TLS 使用。当你以TLS 函式设定或取出数据,事实上你真正面对的就是那64 DWORDs好,现在我们知道了原来那些“对各线程有不同意义的全局变量”是存放在线程各自的TDB中阿。 
 

    接下来你也许会问:我怎么存取这64DWORDS呢?我又怎么知道哪个DWORDS被占用了,哪个没有被占用呢?首先我们要理解这样一个事实:系统之所以给我们提供TLS这一功能,就是为了方便的实现“对各线程有不同意义的全局变量”这一功能;既然要达到“全局变量”的效果,那么也就是说每个线程都要用到这个变量,既然这样那么我们就不需要对每个线程的那64DWORDS的占用情况分别标记了,因为那64DWORDS中的某一个一旦占用,是所有线程的那个DWORD都被占用了,于是KERNEL32 使用两个DWORDs(总共64 个位)来记录哪一个slot 是可用的、哪一个slot 已经被用。这两个DWORDs 可想象成为一个64 位数组,如果某个位设立,就表示它对应的TLS slot 已被使用。这64 TLS slot 数组存放在process database 中(在进程一节中的PDB结构中我们列出了那两个DWORDs)。 
 

下面的四个函数就是对TLS进行操作的:  

1TlsAlloc  

上面我们说过了KERNEL32 使用两个DWORDs(总共64 个位)来记录哪一个slot 是可用的、哪一个slot 已经被用。当你需要使用一个TLS slot 的时候,你就可以用这个函数将相应的TLS slot位置1。  

2TlsSetValue  

TlsSetValue 可以把数据放入先前配置到的TLS slot 中。两个参数分别是TLS slot 索引值以及欲写入的数据内容。TlsSetValue 就把你指定的数据放入64 DWORDs 所组成的数组(位于目前的thread database)的适当位置中。  

3TlsGetValue  

这个函数几乎是TlsSetValue 的一面镜子,最大的差异是它取出数据而非设定数据。和TlsSetValue 一样,这个函数也是先检查TLS 索引值合法与否。如果是,TlsGetValue 就使用这个索引值找到64 DWORDs 数组(位于thread database 中)的对应数据项,并将其内容传回。  

4TlsFree  

这个函数将TlsAlloc TlsSetValue 的努力全部抹消掉。TlsFree 先检验你交给它的索引值是否的确被配置过。如果是,它将对应的64 TLS slots 位关闭。然后,为了避免那个已经不再合法的内容被使用,TlsFree 巡访进程中的每一个线程,把0 放到刚刚被释放的那个TLS slot 上头。于是呢,如果有某个TLS 索引后来又被重新配置,所有用到该索引的线程就保证会取回一个0 值,除非它们再调用TlsSetValue

 

 

 

 

 

 

 

Thread Local Storage

All threads of a process share its virtual address space. The local variables of a function are unique to each thread that runs the function. However, the static and global variables are shared by all threads in the process. With thread local storage (TLS), you can provide unique data for each thread that the process can access using a global index. One thread allocates the index, which can be used by the other threads to retrieve the unique data associated with the index.

The constant TLS_MINIMUM_AVAILABLE defines the minimum number of TLS indexes available in each process. This minimum is guaranteed to be at least 64 for all systems. The limits are as follows:

SystemLimit
Windows Server 2003, Windows XP, and Windows 20001088 indexes per process
Windows Me/9880 indexes per process
Windows NT and Windows 9564 indexes per process

When the threads are created, the system allocates an array of LPVOID values for TLS, which are initialized to NULL. Before an index can be used, it must be allocated by one of the threads. Each thread stores its data for a TLS index in a TLS slot in the array. If the data associated with an index will fit in an LPVOID value, you can store the data directly in the TLS slot. However, if you are using a large number of indexes in this way, it is better to allocate separate storage, consolidate the data, and minimize the number of TLS slots in use.

The following diagram illustrates how TLS works.

The process has two threads, Thread 1 and Thread 2. It allocates two indexes for use with TLS, gdwTlsIndex1 and gdwTlsIndex2. Each thread allocates two memory blocks (one for each index) in which to store the data, and stores the pointers to these memory blocks in the corresponding TLS slots. To access the data associated with an index, the thread retrieves the pointer to the memory block from the TLS slot and stores it in the lpvData local variable.

It is ideal to use TLS in a dynamic-link library (DLL). Use the following steps to implement TLS in a DLL:

  1. Declare a global variable to contain the TLS index. For example:

    static DWORD gdwTlsIndex;
  2. Use the TlsAlloc function during initialization to allocate the TLS index. For example, include the following call in the DllMain function during DLL_PROCESS_ATTACH:

    gdwTlsIndex = TlsAlloc();
  3. For each thread using the TLS index, allocate memory for the data, then use the TlsSetValue function to store the address of the memory block in the TLS slot associated with the index. For example, include the following code in your DllMain during DLL_THREAD_ATTACH:

    LPVOID lpvBuffer;lpvBuffer = (LPVOID) LocalAlloc(LPTR, 256);TlsSetValue(gdwTlsIndex, lpvBuffer);
  4. When a function requires access to the data associated with a TLS index, specify the index in a call to the TlsGetValue function. This retrieves the contents of the TLS slot for the calling thread, which in this case is a pointer to the memory block for the data. For example, include the following code in any of the functions in your DLL:

    LPVOID lpvData;lpvData = TlsGetValue(gdwTlsIndex);
  5. When each thread no longer needs to use a TLS index, it must free the memory whose pointer is stored in the TLS slot. When all threads have finished using a TLS index, use the TlsFree function to free the index. For example, use the following code in your DllMain during DLL_THREAD_DETACH:

    lpvBuffer = TlsGetValue(gdwTlsIndex);LocalFree((HLOCAL) lpvBuffer);

    and the following code during DLL_PROCESS_DETACH:

    TlsFree(gdwTlsIndex);

For examples illustrating the use of thread local storage, see Using Thread Local Storage and Using Thread Local Storage in a Dynamic Link Library.

 

 

 

 

就是多个线程想用同一个全局变量。

1)线程函数A,里面要读写一个对应的文件,如果这个文件的HANDLE作为局部变量的话,只在这个线程函数内使用的话就不需要TLS。

2)但是如果A是要调用函数B(B不是线程函数,是普通函数),B里面要访问文件的HANDLE。

    a)如果把这个文件HANDLE作为局部变量,函数B访问不到。

    b)把handle作为参数传给函数B。这样可以解决,但是有时需要的参数太多,需要一种全局变量的机制。

    c)要作为全局变量的话,如果BeginThread(A) 两次的话,每个A的实例线程(假设两个线程实例A1,A2)里面的对应的文件HANDLE(Handle1,Handle2)是不同的。

    那通过一个全局变量的名字就不能存储两个HANDLE,但是又不能用两个全局变量名,因为B的代码是唯一的,比如它对这个文件进行操作时就是GetFile(gFileHandle)。这时候就需要一种技术能一个名字(gFileHandle)能存储两个变量,自动根据当前线程来获得相对应的变量。比如如果是线程A1内调用B的代码,B就会自动根据当前线程的TIB把gFileHandle翻译成Handle1。

 

 

 

原创粉丝点击