Sync model for Go

Source: Internet
Author: User

First look at a piece of code, which is an example of the Go Memory model article

?

var a, b int

?

Func f () {

???? A = 1

???? b = 2

}

Func g () {

???? Print (b)

???? Print (a)

}

Func Main () {

???? Go f ()

???? G ()

}

?

In fact, we need to say two points:

First: The statements within the function f may be executed in a random order, due to compiler optimizations, since the two statements do not have any dependencies on the inside of the function, which the compiler can do to improve operational efficiency

Second: This article says that the more radical compiler might delete the two statements of A=1 b=2 because it looks completely useless. From the local point of view it seems like this, but A and B are global variables, so aggressive appropriate? Maybe it's just a talk.

?

The meaning of the article is very clear, if there is a dependency, then explicitly write it out, otherwise the compiler does not know Ah!

Sync model for Go

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.