|
Using Thread Local Storage in a Dynamic-Link Library
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 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 a pointer to the allocated memory.
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. Each thread can use the TLS index in a call to TlsGetValue to retrieve the pointer to its own block of memory.
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.
The TLS index is stored in a global variable, making it available to all of
the DLL functions. The following 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.
static DWORD dwTlsIndex; // address of shared memory
// DllMain() is the entry-point function for this DLL.
BOOL DllEntryPoint(HINSTANCE hinstDLL, // DLL module handle
DWORD fdwReason, // reason called
LPVOID lpvReserved) // reserved
{
LPVOID lpvData;
BOOL fIgnore;
switch (fdwReason) {
// The DLL is loading due to process
// initialization or a call to LoadLibrary.
case DLL_PROCESS_ATTACH:
// Allocate a TLS index.
if ((dwTlsIndex = TlsAlloc()) == 0xFFFFFFFF)
return FALSE;
// No break: Initialize the index for first thread.
// The attached process creates a new thread.
case DLL_THREAD_ATTACH:
// Initialize the TLS index for this thread.
lpvData = (LPVOID) LocalAlloc(LPTR, 256);
if (lpvData != NULL)
fIgnore = TlsSetValue(dwTlsIndex, lpvData);
break;
// The thread of the attached process terminates.
case DLL_THREAD_DETACH:
// Release the allocated memory for this thread.
lpvData = TlsGetValue(dwTlsIndex);
if (lpvData != NULL)
LocalFree((HLOCAL) lpvData);
break;
// The DLL unloading due to process termination or call to
FreeLibrary.
case DLL_PROCESS_DETACH:
// Release the allocated memory for this thread.
lpvData = TlsGetValue(dwTlsIndex);
if (lpvData != NULL)
LocalFree((HLOCAL) lpvData);
// Release the TLS index.
TlsFree(dwTlsIndex);
break;
default:
break;
}
return TRUE;
UNREFERENCED_PARAMETER(hinstDLL);
UNREFERENCED_PARAMETER(lpvReserved);
}
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 the LoadLibrary function is called, so TLS memory is not allocated for these threads. The
following example solves this problem by checking the value returned by the TlsGetValue function and allocating memory if the value indicates that the TLS slot for
this thread is not set.
LPVOID lpvData;
// Retrieve a data pointer for the current thread.
lpvData = TlsGetValue(dwTlsIndex);
// If NULL, allocate memory for this thread.
if (lpvData == NULL) {
lpvData = (LPVOID) LocalAlloc(LPTR, 256);
if (lpvData != NULL)
TlsSetValue(dwTlsIndex, lpvData);
}
| Last news from Greatis Software |
 |
|
Nostalgia .Net |
|
.Net is powerful, but not all-powerful, so sometimes we need to use Win32 API for our .Net applications. It's simple enough with Platform Invoke if you have Win32 skill, but we do not always have time to dig the ancient documentation, declare the special types that are compatible with Win32, find the values of the Win32's constants and so on. Nostalgia .Net offers several simple-to-use classes, and components that will allow you to forget about the headache of Win32 and just use the power of Win32 in your application the same way as you use the native. Net classes. More » |
| Recommended software for developers |
 |
|
Ultimate Pack |
|
Component pack for Delphi and C++ Builder that contains runtime form designer, runtime object inspector, print suite and much more for the very special price. More » |
 |
|
Form Designer .Net |
|
Unique runtime form design solution that allows to edit any form in .Net WinForms application at runtime with full source codes for only 300 euro! More » |
 |
|
Print Suite .Net |
|
Print Suite .Net is a set of components for easy printing texts, images and grids from your WinForms applications. Full C# source codes are available More » |
 |
|
Gradient Controls .Net |
|
Gradient Controls .Net offers controls with gradient background feature. Labels, panels and so on... Full C# source codes are available More » |
 |
|
Greatis iGrid |
|
iGrid plots drawing grid right over your desktop, so you can use it everywhere, with any drawing application without any special plugins for different graphic editors. More » |
All the contacts and projectsDmitry Vasiliev (just.dmitry)
Related LinksSoftware for Visual Studio .NET developers Software for Delphi and C++ Builder developers Software for Visual Basic 6 developers Delphi Tips&Tricks MegaDetailed.NET More Online Helps Win32 Programmer's Reference Win32 Multimedia Programmer's Reference OLE Programmer's Reference Microsoft Windows Pen API Programmer's Reference Microsoft Windows Sockets 2 Reference Microsoft Windows Telephony API (TAPI) Programmer's Reference Unix Manual Pages
|