0

i am looking through the documentation on thread local storage but i believe its poorly written.

https://learn.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. 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. And the pointers to the memory blocks are retrieved from the TLS slots while being stored in the local variable.

My question is exactly what values are stored in the TLS slots, memory address or actual data values? 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? 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?

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

3 Answers 3

1

The TLS slots - the ones in your diagram - are a fixed-size array of LPVOID-sized variables in the thread's information block. 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.

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you for the clear explanation. Accepted answer, love the name :)
1

My question is exactly what values are stored in the TLS slots, memory address or actual data values?

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. 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.

This is a technique known as Small Object Optimization, 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. 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."

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? 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?

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.

Comments

0

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. See https://en.cppreference.com/w/cpp/keyword/thread_local

This is portable and is a lot easier to use.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.