Question Details

No question body available.

Tags

c++ windows multithreading visual-c++ thread-local-storage

Answers (1)

Accepted Answer Available
Accepted Answer
October 29, 2025 Score: 18 Rep: 35,415 Quality: Expert Completeness: 80%

Do this in the following way:

ULONG tlsindex;

void CALLBACK OnTls(PVOID, DWORD Reason, PVOID);

static const PIMAGETLSCALLBACK gtlscb[] = { OnTls, 0 };

#pragma constseg(".rdata$T")

EXTERNC const IMAGETLSDIRECTORY tlsused = { (ULONGPTR)0, // Start of TLS data (ULONGPTR)0, // End of TLS data (ULONGPTR)&tlsindex, // Address of tlsindex (ULONGPTR)gtlscb, // Pointer to call back array }; #pragma constseg()

#ifdef WIN64 pragma(comment(linker, "/include:tlsused")) #else pragma(comment(linker, "/include:tlsused")) #endif

More information is in this answer: TLS Callback in Windows

Also from PE Format:

The Microsoft run-time library facilitates this process by defining a memory image of the TLS directory and giving it the special name "tlsused" (Intel x86 platforms) or "tls_used" (other platforms). The linker looks for this memory image and uses the data there to create the TLS directory. Other compilers that support TLS and work with the Microsoft linker must use this same technique.