Expand Minimize

TlsAlloc 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, because each thread receives its own slot for the index.


DWORD WINAPI TlsAlloc(void);


This function has no parameters.

Return value

If the function succeeds, the return value is a TLS index. The slots for the index are initialized to zero.

If the function fails, the return value is TLS_OUT_OF_INDEXES. 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. The value of the TLS index should be treated as an opaque value; do not assume that it is an index into a zero-based array.

TLS indexes are typically allocated during process or dynamic-link library (DLL) initialization. When a TLS index is allocated, its storage slots are initialized to NULL. After a TLS index has been allocated, each thread of the process can use it to access its own TLS storage slot. To store a value in its TLS 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.

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


For an example, see Using Thread Local Storage or Using Thread Local Storage in a Dynamic-Link Library.


Minimum supported client

Windows XP [desktop apps only]

Minimum supported server

Windows Server 2003 [desktop apps only]


WinBase.h on Windows XP, Windows Server 2003, Windows Vista, Windows 7, Windows Server 2008, and Windows Server 2008 R2 (include Windows.h);
Processthreadsapi.h on Windows 8 and Windows Server 2012





See also

Process and Thread Functions
Thread Local Storage



Community Additions

© 2014 Microsoft