views:

408

answers:

3

How, under Windows and Linux, given a thread handle can I determine the beginning and end of its corresponding static thread local storage block?

+1  A: 

See MSDN for more info about the various thread-local storage-related functions. You can use TlsGetValue() to get a specific TLS value; don't go poking around in memory, even if it works - you're just invoking undefined behavior which happens to work but could happen to break in future versions of Windows.

Adam Rosenfield
I did poke around the MSDN thread documentation, but could not find anything about determining this information.
Walter Bright
+2  A: 

You can find this information in the Thread Information Block. Please note, however, that doing so is accessing undocumented structures and is thus highly non-portable and likely to break in future version of Windows. It appears that you can get the current thread's thread-local storage arena from the TIB; I'm not sure who you'd get another thread's TLS.

You could try digging into the Process Environment Block (PEB), at offset 0x30 in the TIB, but again, you're dealing with undocumented structures that are subject to change.

Adam Rosenfield
+1 for the warnings
BCS
+2  A: 

The Windows Thread Information Block gives the address of the thread local storage array at FS:0x2C, which is an array of pointers to the TLS for each module. The problem is that there is no indication of how large that array is, or how large each of the module TLS data blocks are.

Walter Bright