为什么此代码未定义行为? [英] Why is this code Undefined Behavior?
问题描述
var x int
完成:= false
去func(){x = f(...); done = true}
完成== false {}
这是一个Go代码片。我的恶魔告诉我这是UB代码。为什么?
Go Memory Model不保证在
main 程序中会观察到在goroutine中写入x的值。
一个类似错误的程序作为示例在<一个href =http://golang.org/ref/mem#tmp_5 =nofollow noreferrer>去例行破坏。
Go Memory模型还特别呼叫忙等待没有同步作为本节中的一个不正确的习语。
(在您的情况下,不能保证写入 基于频道( 同步包提供了其他方法来等待gorountine在退出main之前结束。 参见Golang示例: This is a Go code piece. My fiend told me this is UB code. Why? As explained in "Why does this program terminate on my system but not on playground?" The Go Memory Model does not guarantee that the value written to x in the goroutine will ever be observed by the (in your case, there is no guarantee that the value written to Here, You need to do some kind of synchronization in the goroutine in order to guarantee that The " Based on a channel ( The sync package provides other means to wait for a gorountine to end before exiting main. See for instance Golang Example Wait until all the background goroutine finish:
这篇关于为什么此代码未定义行为?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋! b
$ b $ p> >在这里,您需要在goroutine中进行某种同步以保证 done <= code>在
main
中循环的
pre $ 为{
<-c // 2
}
c:= make(chan bool)$在
main
中创建,并在goroutine中关闭( close(c)
)。
var w sync.WaitGroup
w.Add(1)
go func(){
//做某事
w.Done()
}
w.Wait()
var x int
done := false
go func() { x = f(...); done = true }
while done == false { }
main
program.
A similarly erroneous program is given as an example in the section on go routine destruction.
The Go Memory Model also specifically calls out busy waiting without synchronization as an incorrect idiom in this section.done
in the goroutine will ever be observed by the main
program) done=true
happens before one of the iterations of the for
loop in main
.while
" (non-existent in Go) should be replaced by, for instance, a channel you block on (waiting for a communication)for {
<-c // 2
}
c := make(chan bool)
) created in main
, and closed (close(c)
) in the goroutine.
var w sync.WaitGroup
w.Add(1)
go func() {
// do something
w.Done()
}
w.Wait()