关于并发的问题,如下代码该如何理解?

yuchen16 · · 965 次点击
“Within a single goroutine, reads and writes must behave as if they executed in the order specified by the program. That is, compilers and processors may reorder the reads and writes executed within a single goroutine only when the reordering does not change the behavior within that goroutine as defined by the language specification. Because of this reordering, the execution order observed by one goroutine may differ from the order perceived by another. For example, if one goroutine executes a = 1; b = 2;, another might observe the updated value of b before the updated value of a.” func setup() { t := new(T) t.msg = "hello, world" g = t } go setup()的实际执行顺序可能是: func setup() { t := new(T) g = t t.msg = "hello, world" } 因为针对上面的例子,这样调换执行顺序并不违背明确的语法规则,要做同步就必须使用go的明确的语法规则,而不是上面的潜规则
#9
更多评论
不能保证 setup 先执行
#1
意思是不能保证setup先返回?
#2