Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

appendix/appendix-a-trap#594

giscus[bot]bot started this conversation inGeneral
Aug 19, 2022· 1 comments· 4 replies
Discussion options

appendix/appendix-a-trap

Go语言高级编程

https://chai2010.cn/advanced-go-programming-book/appendix/appendix-a-trap.html

You must be logged in to vote

Replies: 1 comment 4 replies

Comment options

不同Goroutine之间不满足顺序一致性内存模型

You must be logged in to vote
4 replies
@shijiehehaigiscus
Comment options

不同Goroutine之间不满足顺序一致性内存模型 这个例子 var msg string
var done bool

func setup() {
msg = "hello, world"
done = true
}

func main() {
go setup()
for !done {
}
println(msg)
}

@shijiehehaigiscus
Comment options

这个例子 我的go版本是1.16.6 是可以稳定输出"hello, world"的

@ByteByteDancegiscus
Comment options

多核CPU,可能出现协程a更改的变量,比如msg,在更新到内存前,就被b协程读到了

@lzwttygiscus
Comment options

在多核处理器的计算机上,main Goroutine 和 setup Goroutine 可能在不同的处理器核心上并发执行。每个处理器核心都有自己的缓存,当执行程序时,处理器可能会将一些变量缓存在处理器的寄存器或本地缓存中,而不是立即将它们写回主内存。因此,不同 Goroutine 之间可能会看到不一致的内存状态。
在这个例子中,main Goroutine 可能在 setup Goroutine 将 msg 设置为 "hello, world" 之前,就观察到 done 变量已经变为 true。这可能是因为 done 变量的更新已经写回主内存,但 msg 变量的更新仍然缓存在 setup Goroutine 所在的处理器核心的寄存器或本地缓存中。因此,main Goroutine 看到的 msg 变量仍然是空字符串或未初始化的值。--来自gtp4

Sign up for freeto join this conversation on GitHub. Already have an account?Sign in to comment
Category
General
Labels
None yet
3 participants
@shijiehehai@ByteByteDance@lzwtty

[8]ページ先頭

©2009-2025 Movatter.jp