Threads and Memory
These terms refer to the memory aspects of threading:
Shared variables—All threads within the same process share the address-space of that process. Globally-named and static variables are implicitly shared among all threads within a process. Object instances dynamically created on the heap or automatically created on the stack can be explicitly shared by passing references or addresses between threads.
Local variables—Each thread possesses its own stack within the process address space. Non-static, function-scope variables are automatically allocated and constructed in a thread’s stack as their definitions are encountered. When the thread’s execution flow causes the variables to go out of scope, they are destructed and de-allocated. These automatic object instances are generally considered to be local to the thread, although they can still be accessed by other threads if shared by reference or address.
Thread-local or thread-specific storage—The life span and accessibility of a local variable on a thread’s stack is limited by its scope (it only exists within the scope of the function where it was declared, for example). Also global instances are always shared between all threads. Therefore, another kind of storage is required when threads need to maintain a thread-local instance of some object, but also need to share this instance between the functions being executed by the thread. This capability is provided by thread-local storage (also called thread-specific storage). Thread-local storage is an alternative form of storage maintained and accessed on a per-thread basis, but with a life-span that is independent of normal stack scope. Thread-local storage can also possess a global name that is shared among all functions within a thread.