In-depth understanding of computer systems--12th: Sharing variables in multiple threads

Source: Internet
Author: User
Tags define local

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.