Skip to content

关于 “1.5.3 顺序一致性内存模型”中的疑问 #604

Open
@JOJO0527

Description

@JOJO0527
var a string
var done bool

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

func main() {
	go setup()
	for !done {}
	print(a)
}

但是 Go 语言并不保证在 main 函数中观测到的对 done 的写入操作发生在对字符串 a 的写入的操作之后,因此程序很可能打印一个空字符串。更糟糕的是,因为两个线程之间没有同步事件,setup线程对 done 的写入操作甚至无法被 main 线程看到,main函数有可能陷入死循环中。

我明白这里的顺序一致性内存模型的含义,如果来Java里面的知识做对比的话,应该就是volatile关键字的作用。但是我在Go1.18 arm中运行上述例子,无法重现main函数陷入死循环的情景,想请教下如果想验证的话,有什么实验可以验证呢?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions