MSDN:Thread Local Storage

原创 2011年01月10日 14:03:00

FW:http://msdn.microsoft.com/en-us/library/ms686749.aspx

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 maximum number of indexes per process is 1,088.

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. For a code example illustrating the use of thread local storage, see Using Thread Local Storage.

ms686749.tls(en-us,VS.85).png

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). For an example, see Using Thread Local Storage in a Dynamic Link Library.

Related Topics
Thread Local Storage (Visual C++)
Using Thread Local Storage
Using Thread Local Storage in a Dynamic Link Library

相关文章推荐

[并发并行]_[C/C++]_[使用线程本地存储Thread Local Storage(TLS)调用复制文件接口的案例]

使用场景: 1. 在复制文件时,一般都是一个线程调用一个接口复制文件,这时候需要缓存数据,如果每个文件都需要创建独立的缓存,那么内存碎片是很大的. 如果创建一个static的内存区,当多线程调用同一个...

TLS(thread - local storage)线程局部存储

线程局部存储是一个使用很方便的存储线程局部数据的系统。利用TLS机制可以为进程中所有的线程关联若干数据,各个线程通过由TLS分配的全部索引来访问与自己关联的数据。这样,每个线程都可以有线程局部的静态存...

线程的局部存储tls (thread local storage)

线程局部存储tls(thread local storage) 线程的局部存储就是让一个全局的变量或者静态变量在每一个线程都有自己的立足之地. 动态TLS 首先调用API函数,获取当前进程可用的TLS...

线程局部存储 Thread Local Storage

1 由于多个线程使用同一个变量,各个线程     都对变量进行操作,那么变量的值会被不同     线程操作覆盖。                 通常   变量A                  ...
  • rankun1
  • rankun1
  • 2016年02月16日 20:29
  • 176

Thread Local Storage (TLS)

Thread Local Storage (TLS) Visual Studio 2015 Other Versions   For t...

[并发并行]_[C/C++]_[使用线程本地存储Thread Local Storage(TLS)-win32线程和pthread线程比较]

场景: 1. 需要统计某个线程的对象上创建的个数. 2. 当创建的堆空间需要根据线程需要创建和结束时销毁时. 3. 因为范围是线程只能看到自己的存储数据,所以不需要临界区或互斥量来维护自己的堆内存....

pthread的TLS(THREAD LOCAL STORAGE)

TLS全称为Thread Local Storage,是系统为解决一个进程中多个线程同时访问全局变量而提供的机制。...

Thread Local Storage

Source: http://www.nynaeve.net/?p=180 ThreadLocal Storage, part 1: Overview Windows, likepractical...

TLS:Thread Local Storage

TLS全称为Thread Local Storage,是Windows为解决一个进程中多个线程同时访问全局变量而提供的机制。TLS可以简单地由操作系统代为完成整个互斥过程,也可以由用户自己编写控制信号...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:MSDN:Thread Local Storage
举报原因:
原因补充:

(最多只允许输入30个字)