If a DLL declares any nonlocal data or object as __declspec( thread ), it can cause a protection fault if dynamically loaded. After the DLL is loaded withLoadLibrary, it causes system failure whenever the code references the nonlocal__declspec( thread ) data. Because the global variable space for a thread is allocated at run time, the size of this space is based on a calculation of the requirements of the application plus the requirements of all the DLLs that are statically linked. When you useLoadLibrary, there is no way to extend this space to allow for the thread local variables declared with__declspec( thread ). Use the TLS APIs, such asTlsAlloc, in your DLL to allocate TLS if the DLL might be loaded withLoadLibrary.
http://msdn.microsoft.com/en-us/library/2s9wt68x(v=vs.80).aspx
Using Thread Local Storage in a Dynamic-Link Library
16 out of 32 rated this helpful- Rate this topic
This section shows the use of a DLL entry-point function to set up a thread local storage (TLS) index to provide private storage for each thread of a multithreaded process.
The TLS index is stored in a global variable, making it available to all of the DLL functions. This example assumes that the DLL's global data is not shared, because the TLS index is not necessarily the same for each process that loads the DLL.
The entry-point function uses the TlsAlloc function to allocate a TLS index whenever a process loads the DLL. Each thread can then use this index to store a pointer to its own block of memory.
When the entry-point function is called with the DLL_PROCESS_ATTACH value, the code performs the following actions:
- Uses the TlsAlloc function to allocate a TLS index.
- Allocates a block of memory to be used exclusively by the initial thread of the process.
- Uses the TLS index in a call to the TlsSetValue function to store the address of the memory block in the TLS slot associated with the index.
Each time the process creates a new thread, the entry-point function is called with the DLL_THREAD_ATTACH value. The entry-point function then allocates a block of memory for the new thread and stores a pointer to it by using the TLS index.
When a function requires access to the data associated with a TLS index, specify the index in a call to theTlsGetValue 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. When a process uses load-time linking with this DLL, the entry-point function is sufficient to manage the thread local storage. Problems can occur with a process that uses run-time linking because the entry-point function is not called for threads that exist before theLoadLibrary function is called, so TLS memory is not allocated for these threads. This example solves this problem by checking the value returned by theTlsGetValue function and allocating memory if the value indicates that the TLS slot for this thread is not set.
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 theTlsFree function to release the index.
When a thread terminates, the entry-point function is called with the DLL_THREAD_DETACH value and the memory for that thread is freed. When a process terminates, the entry-point function is called with the DLL_PROCESS_DETACH value and the memory referenced by the pointer in the TLS index is freed.
C++
Copy
The following code demonstrates the use of the DLL functions defined in the previous example.
C++
Copy
Related topics
Using Thread Local Storage
http://msdn2.microsoft.com/en-us/library/ms686997.
aspx