It is often necessary for every thread of a process to have its own copy of read/write data. To provide this data, Windows CE supports thread local storage (TLS). TLS enables each thread to allocate the objects it owns in memory and to manipulate thread-specific data. You can use the TlsAlloc, TlsSetValue, TlsGetValue, and TlsFree functions to allocate and free memory for thread storage.
When a dynamic-link library (DLL) attaches to a process, the DLL uses TlsAlloc to allocate a TLS index. The DLL then allocates dynamic storage and uses the TLS index in a call to TlsSetValue to store the address in the TLS slot. The TLS index is stored in a global or static variable of the DLL. Each time the DLL attaches to a new thread of the process, the DLL allocates dynamic storage for the new thread and uses the TLS index in a call to TlsSetValue to store the address in the TLS slot.
Each time an initialized thread makes a DLL call that requires the data in its dynamic storage, the DLL uses the TLS index in a call to TlsGetValue to retrieve the address of the dynamic storage for that thread.
TLS functions supported by Windows CE are described in the following table.
Function | Description |
---|---|
TlsAlloc | Allocates a TLS index. The index is available to any thread in the process for storing and retrieving thread-specific values. |
TlsFree | Releases the TLS index, making it available for reuse. |
TlsGetValue | Retrieves the value pointed to by the TLS index. |
TlsSetValue | Stores a value in the slot pointed to by the TLS index. |
Use the following criteria to help you decide which memory allocation method is best in particular situations: