Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

请教一个go101中的例子 不太理解 初学go 见谅问题比较浅显可能 #107

Closed
GodGavin opened this issue Aug 20, 2021 · 1 comment
Labels
question Further information is requested

Comments

@GodGavin
Copy link

GodGavin commented Aug 20, 2021

在Go的内存顺序保证一节中,讲了一个通道关闭的例子,如下
下面是一个通道关闭的例子。在这个例子中,赋值语句k = 1的执行保证在赋值语句y = 1执行之前结束,但不能保证在赋值语句x = 1执行之前结束。

func f6() {
    var k, x, y int
    c := make(chan bool, 1)
    go func() {
        c <- true
        k = 1
        close(c)
    }()
    go func() {
        <-c
        x = 1
        <-c
        y = 1
    }()
}

我自己写了一遍跑了下 但是还是不能理解为什么 赋值语句k = 1的执行保证在赋值语句y = 1执行之前结束,
感觉

        k = 1
        close(c)

        x = 1
        <-c
        y = 1

这两部分的执行顺序完全不能确定啊 请教我谢谢

@go101
Copy link
Collaborator

go101 commented Aug 21, 2021

微信上已解答。这里先关闭了。

@go101 go101 closed this as completed Aug 21, 2021
@go101 go101 added the question Further information is requested label Aug 21, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested
Projects
None yet
Development

No branches or pull requests

2 participants