Reputation: 29
i have few questions about TLS, and how it differes from local variables, global variables and local variables.
in general, i can't get the the different between local variable and thread local variable.
Upvotes: 0
Views: 711
Reputation: 61361
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.
If you try to access it from another thread after the creating thread quits, that's undefined behavior.
That's an implementation detail. But most likely, on the heap.
Um, that's up to you. In theory, pretty much everything they normally do with TLV you can also do without...
As long as the thread is running.
Define "exclusive".
Upvotes: 1