0

i have few questions about TLS, and how it differes from local variables, global variables and local variables.

  1. thread local variables are accessible only for the owner thread, and local variables are accessible only for the function?
  2. where the thread local variables are stored?
  3. when we will want to use thread local variables?
  4. what is the life time of thread local variable?
  5. is thread local variable exlusive to the thread?

in general, i can't get the the different between local variable and thread local variable.

Eran Nahshon
  • 29
  • 1
  • 6

1 Answers1

1

Local variables exist in a function. Once the function returns, they're gone. Thread variables exist on a thread; once the thread quits, they're gone. In terms of lifetime, you could say that TLV are equivalent to local variables of the thread startup function, except you don't have to explicitly pass the reference to them to the code further down.

Note that there are two ways of employing TLV in a native Windows program. Microsoft C++ has a __declspec(thread) modifier, and also there's a family of Windows API functions - TlsAlloc() and the like.

  1. Other threads can access thread local variables if a pointer/reference is somehow passed to them. However, the vanilla access methods will return the current thread's instance.

If you try to access it from another thread after the creating thread quits, that's undefined behavior.

  1. That's an implementation detail. But most likely, on the heap.

  2. Um, that's up to you. In theory, pretty much everything they normally do with TLV you can also do without...

  3. As long as the thread is running.

  4. Define "exclusive".

Seva Alekseyev
  • 59,826
  • 25
  • 160
  • 281
  • first of all, thank you for the detailed answer. so to summerize, i can say that TLV are local variables, in the scope of the thread instead of a function, and they stored mainly on the heap rather then the stack. is it possible to allocate the TLV on the thread's stack? please notice me if there is a difference i have missed – Eran Nahshon Sep 26 '18 at 20:10
  • by exlusive i meant if the TLV is only accessable by the thread – Eran Nahshon Sep 26 '18 at 20:20
  • What do you mean by accessable? In the sense that trying to access will crash the program? No, there's no protection of that sort. – Seva Alekseyev Sep 26 '18 at 20:24
  • Just like local variables are accessible only in their function, is TLV is accessible only in the thread? From your answers i learned that its not the case, and if i pass a pointer to another thread he can access another thread's local variables and storage. Please correct me if i'm wrong – Eran Nahshon Sep 26 '18 at 20:28
  • Function local variables are accessible by other functions, too. You can pass a pointer/reference to them. Same with TLV. – Seva Alekseyev Sep 26 '18 at 20:30
  • So you can summerize for me the differences between TLV and local variables? – Eran Nahshon Sep 26 '18 at 20:32
  • I can't summarize the difference, because I don't know what kind of difference matters to you. Can you summarize the difference between apples and oranges? – Seva Alekseyev Sep 26 '18 at 20:48
  • I mean, what TLV gives me that loacl variables can't? And the differences i'm aiming to are storing location (probably stack vs heap), scope (no such difference), when to use. Is TLV is just more convenient? – Eran Nahshon Sep 26 '18 at 20:57
  • They have global visibility. You can use TLVs without explicitly passing them around. It's more convenient. – Seva Alekseyev Sep 26 '18 at 21:00
  • That's all? Or is there more? – Eran Nahshon Sep 26 '18 at 21:01
  • Are you studying, or trying to solve a specific problem with a program? – Seva Alekseyev Sep 26 '18 at 21:03
  • Studying the concept of tls callbacks. For this, i want to understand perfectly the mechanism of TLS, and variable types in general – Eran Nahshon Sep 26 '18 at 21:04
  • Okay, so we're in the hacking/reverse engineering area now. While you're here, forget about high level language concepts like scope and visibility. There's memory and registers, and that's it. Things like scope are limitations imposed by high level languages to keep you from self inflicted harm. – Seva Alekseyev Sep 26 '18 at 21:14
  • Yes, but it got me curious. Thanks alot seva, i belive i got my answers and got the thing clear – Eran Nahshon Sep 27 '18 at 04:25
  • Yes, but it got me curious. Thanks alot seva, i belive i got my answers and got the thing clear – Eran Nahshon Sep 27 '18 at 04:25