I found a contradiction in MSDN regarding initial values โโfor local thread storage. This page says:
When threads are created, the system allocates an array of LPVOID values โโfor TLS, which are initialized to NULL.
This makes me think that if I call TlsGetValue with a valid index from a thread that never called TlsSetValue for the same index, then I should get a null pointer.
This page , however, says:
The programmerโs task is to ensure that the thread calls TlsSetValue before calling TlsGetValue.
This suggests that you cannot rely on the value returned from TlsGetValue unless you are sure that it is explicitly initialized using TlsSetValue.
However, the second page at the same time reinforces the initialized-zero behavior, also saying:
The data stored in the TLS slot may have a value of 0 because it still has an initial value or because the stream, called the TlsSetValue function, is 0.
So, I have two statements that the data is initialized to zero (or 0), and one says that I should initialize it explicitly before reading the value. Experimentally, the values โโreally seem to be initialized null pointers automatically, but I donโt know if I can just succeed, and whether it will always be so.
DLL, DLL_THREAD_ATTACH. :
LPVOID pMyData = ::TlsGetValue(g_index);
if (pMyData == nullptr) {
pMyData = /* some allocation and initialization*/;
// bail out if allocation or initialization failed
::TlsSetValue(g_index, pMyData);
}
DoSomethingWith(pMyData);
? , ?
: , TlsAlloc . , , .