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.
- 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.
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".