Shared variables in multithreaded programs:
A thread is shared when and only if multiple threads refer to an instance of this variable.
12.4.1 Thread Memory model
(1) The registers are not shared, and the virtual memory is always shared
(2) The stack of threads is stored in the stack area of the virtual address space and is usually accessed independently by the corresponding thread.
(3) But line stacks is not fortified by other threads. If a thread gets a pointer to another line stacks in some way, then he can read and write the stack
Any part of it.
The 12.4.2 maps the variables to memory.
Global variables:
(1) A global variable is a variable that is defined outside of a function.
(2) At run time, the read-write area of the virtual memory contains only one instance of the global variable, which can be referenced by any thread.
Local Automatic variables:
(1) is a local variable that is defined inside the function but does not have a static variable.
(2) Each thread contains an instance of all of its own local automatic variables, even if all threads share a routine.
Local Static variables:
(1) Define local variables that are static inside the function
(2) As with the global variable effect, there is only one instance.
12.4.3 Shared variables
(1) When and only if one of its instances is referenced by a live thread.
(2) Local automatic variables can also be shared.
In-depth understanding of computer systems--12th: Sharing variables in multiple threads