Export (0) Print
Expand All
This topic has not yet been rated - Rate this topic

TlsAlloc

Windows Mobile 6.5
A version of this page is also available for
4/8/2010

This function allocates a thread local storage (TLS) index. Any thread of the process can subsequently use this index to store and retrieve values that are local to the thread.


DWORD TlsAlloc(void);

None.

A TLS index indicates success. TLS_OUT_OF_INDEXES (0xFFFFFFFF) indicates failure. To get extended error information, call GetLastError.

The threads of the process can use the TLS index in subsequent calls to the TlsFree, TlsSetValue, or TlsGetValue functions.

TLS indexes are typically allocated during process or DLL initialization. When allocated, each thread of the process can use a TLS index to access its own TLS storage slot. To store a value in its slot, a thread specifies the index in a call to TlsSetValue.

The thread specifies the same index in a subsequent call to TlsGetValue, to retrieve the stored value.

The constant TLS_MINIMUM_AVAILABLE defines the minimum number of TLS indexes available in each process. This minimum is guaranteed to be at least 64 for all systems.

TLS indexes are not valid across process boundaries. A DLL cannot assume that an index assigned in one process is valid in another process.

The following list shows how a DLL might use TlsAlloc, TlsSetValue, TlsGetValue, and TlsFree:

  • When a 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.
    This concludes the per-thread initialization for the initial thread of the process. 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.
    This concludes the per-thread initialization for the new thread.
  • Each time an initialized thread makes a DLL call requiring 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.

For additional information on thread local storage, see Kernel Overview.

Headerwinuser.h
Librarycoredll.lib
Windows Embedded CEWindows CE 1.0 and later
Windows MobileWindows Mobile Version 5.0 and later
Did you find this helpful?
(1500 characters remaining)
Thank you for your feedback

Community Additions

ADD
Show:
© 2014 Microsoft. All rights reserved.