简体   繁体   English

线程本地存储进程

[英]Thread local storage processes

i am looking through the documentation on thread local storage but i believe its poorly written.我正在查看有关线程本地存储的文档,但我认为它写得不好。

https://docs.microsoft.com/en-us/windows/win32/procthread/thread-local-storage https://docs.microsoft.com/en-us/windows/win32/procthread/thread-local-storage

It seems like the way they write documentations is in a sporadic order, they will provide an event that happens, then the next sentence they will provide information on what needs to happen before this event.似乎他们编写文档的方式是零星的,他们将提供一个发生的事件,然后下一句他们将提供有关在此事件之前需要发生什么的信息。 And then another event happens, they will explain what needs to happen before this event.然后另一个事件发生,他们会解释在这个事件之前需要发生什么。 But the chain of sequential events is unclear.但顺序事件链尚不清楚。 Often they will provide an object in a sentence, then the next sentence talking about another object while revealing more details about the object before.通常他们会在一个句子中提供一个对象,然后下一个句子谈论另一个对象,同时揭示之前关于该对象的更多细节。

But from my understanding, there's a global index where the index has associated data, a thread allocates the index and other threads can then access it.但是根据我的理解,有一个全局索引,其中索引具有关联数据,一个线程分配索引,然后其他线程可以访问它。 And when threads are created, an array of LPVOID values called the TLS slot are generated.当创建线程时,会生成一个称为 TLS 槽的 LPVOID 值数组。 The data associated with the index are then stored in this array.然后将与索引关联的数据存储在该数组中。

My confusion lies at the last part, it says the Threads allocate memory blocks, the pointers to these memory blocks are then stored in LPVOID TLS slots.我的困惑在于最后一部分,它说线程分配内存块,然后将指向这些内存块的指针存储在 LPVOID TLS 插槽中。 And the pointers to the memory blocks are retrieved from the TLS slots while being stored in the local variable.指向内存块的指针是从 TLS 插槽中检索的,同时存储在局部变量中。

My question is exactly what values are stored in the TLS slots, memory address or actual data values?我的问题是究竟什么值存储在 TLS 插槽、内存地址或实际数据值中? And if memory address pointers, im supposing these addresses are then accessed to get the value stored at the memory block.如果是内存地址指针,我假设然后访问这些地址以获取存储在内存块中的值。

Is it also correct that 2 memory spaces are allocated, one for LPVOID array values and there others for memory space blocks for the indexes?分配 2 个内存空间是否也正确,一个用于 LPVOID 数组值,另一个用于索引的内存空间块? It said if using a large number of indexes and LPVOID arrays, its better to allocate a separate memory space to avoid occupying TLS slots, is this what the memory blocks refer to, data is stored in the memory blocks instead and addresses in slots to avoid data overload in the slots?它说如果使用大量索引和LPVOID数组,最好分配一个单独的内存空间以避免占用TLS插槽,这是内存块所指的,数据存储在内存块中,地址在插槽中以避免插槽中的数据过载?

Reading the documentation is like a puzzle, if anyone can be helpful i would be grateful.阅读文档就像一个难题,如果有人可以提供帮助,我将不胜感激。 Ive shown an image of the illustrated structured they've provided.我已经展示了他们提供的图示结构的图像。

enter image description here在此处输入图片说明

The TLS slots - the ones in your diagram - are a fixed-size array of LPVOID-sized variables in the thread's information block. TLS 插槽(图中的插槽)是线程信息块中 LPVOID 大小的变量的固定大小数组。 Typically they're used to store pointers.通常它们用于存储指针。 The memory pointed to by a pointer stored here is not local to the thread but allocated normally.存储在这里的指针指向的内存不是线程本地的,而是正常分配的。 However since the thread stores the pointer in a TLS slot, which is local to the thread, the memory is effectively private to the thread.然而,由于该线程存储在TLS插槽,这本地线程的指针,该内存是有效线程专用。

If you're talking about the raw Windows API and not compiler-assisted thread local storage, it is not a requirement that the data stored in TLS be pointers, it can be plain numeric values (cast to and from LPVOID) if that makes sense for your scenario.如果您谈论的是原始 Windows API 而不是编译器辅助的线程本地存储,那么存储在 TLS 中的数据不是指针的要求,如果有意义,它可以是纯数字值(转换为 LPVOID 或从 LPVOID 转换)为您的方案。

My question is exactly what values are stored in the TLS slots, memory address or actual data values?我的问题是究竟什么值存储在 TLS 插槽、内存地址或实际数据值中?

Both!两个都!

This is covered by the following paragraph in your link:您的链接中的以下段落涵盖了这一点:

If the data associated with an index will fit in an LPVOID value, you can store the data directly in the TLS slot.如果与索引关联的数据适合 LPVOID 值,您可以将数据直接存储在 TLS 插槽中。 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.但是,如果您以这种方式使用大量索引,则最好分配单独的存储,合并数据,并尽量减少 TLS 插槽的数量。

This is a technique known as S mall O bject O ptimization, where some storage can either be a value or a pointer to a some other memory location depending on the size of what is being stored.这是公知的为S商场öbjectøptimization,其中一些存储可以是一个值或一个指针,这取决于被存储什么尺寸的一些其它存储器位置的技术。 The paragraph is just a roundabout way of saying:该段落只是一种迂回的说法:

"We recommend you use SOO if you can, since the system doesn't look at the value of the pointers." “如果可以,我们建议您使用 SOO,因为系统不会查看指针的值。”

And if memory address pointers, im supposing these addresses are then accessed to get the value stored at the memory block.如果是内存地址指针,我假设然后访问这些地址以获取存储在内存块中的值。

Correct, but only if you choose to use the storage as a pointer.正确,但前提是您选择将存储用作指针。

Is it also correct that 2 memory spaces are allocated, one for LPVOID array values and there others for memory space blocks for the indexes?分配 2 个内存空间是否也正确,一个用于 LPVOID 数组值,另一个用于索引的内存空间块? It said if using a large number of indexes and LPVOID arrays, its better to allocate a separate memory space to avoid occupying TLS slots, is this what the memory blocks refer to, data is stored in the memory blocks instead and addresses in slots to avoid data overload in the slots?它说如果使用大量索引和LPVOID数组,最好分配一个单独的内存空间以避免占用TLS插槽,这是内存块所指的,数据存储在内存块中,地址在插槽中以避免插槽中的数据过载?

Not really, it's just one big table of pointers.不是真的,它只是一张大的指针表。 If you want the pointers to point somewhere meaningful, you'll need to allocate some memory to be pointed at, but there's nothing special about that allocation.如果您希望指针指向某个有意义的地方,则需要分配一些要指向的内存,但该分配没有什么特别之处。

If you intend to write C++ and you do not have a need to use Win32 functions specifically, then you should use the C++11 thread_local .如果您打算编写 C++ 并且您不需要专门使用 Win32 函数,那么您应该使用 C++11 thread_local See https://en.cppreference.com/w/cpp/keyword/thread_localhttps://en.cppreference.com/w/cpp/keyword/thread_local

This is portable and is a lot easier to use.这是便携式的,更容易使用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM