<pre><code>package main
import (
"fmt"
)
func launchRoutine(i int) {
go func(i int) {
fmt.Printf("goroutine : %d\n", i)
go func(i int) {
fmt.Printf("subroutine : %d\n", i)
return
}(i)
return
}(i)
return
}
func main() {
for i := 0; i < 10; i++ {
go launchRoutine(i)
}
<-make(chan int) //holds main goroutine forever
}
</code></pre>
<hr/>**评论:**<br/><br/>enneff: <pre><p>Goroutines don't have parents or children. When you start a goroutine it just executes alongside all other running goroutines. Each goroutine exits only when its function returns.</p>
<p>The only exception to that all goroutines exit when the main goroutine (the one that runs function main) exits. (There is also <a href="https://golang.org/pkg/runtime/#Goexit" rel="nofollow"><code>runtime.Goexit</code></a> which has limited use.)</p></pre>kaeshiwaza: <pre><p>Why this exception that all goroutines exit when the main goroutine exit ?</p></pre>enneff: <pre><p>It's convenient to have a mechanism to exit the program regardless of what goroutines are running. Even though <code>os.Exit</code> can do this, it was a design decision to give the main goroutine this property. It could have gone either way, honestly.</p></pre>tuxlinuxien: <pre><pre><code>goroutine 1 [chan receive]:
main.main()
/go/src/datapro/main.go:25 +0x87
exit status 2
</code></pre>
<p>the deadlock happen at this line</p>
<pre><code><-make(chan int)
</code></pre>
<p>simply because your create and try to read from this channel, but no other goroutine can feed it.</p>
<p>Replace <strong><-make(chan int)</strong> by <strong>select {}</strong> and it will work.</p></pre>infogulch: <pre><blockquote>
<p>Replace <-make(chan int) by select {} and it will work.</p>
</blockquote>
<p>This doesn't work for me in the playground. Still displays a deadlock message: <a href="https://play.golang.org/p/yWqSEIVYE9" rel="nofollow">https://play.golang.org/p/yWqSEIVYE9</a></p></pre>tuxlinuxien: <pre><p>Well, have you checked <strong>sync.WaitGroup</strong> ?</p>
<p><a href="https://play.golang.org/p/XPs0ip_-vx" rel="nofollow">https://play.golang.org/p/XPs0ip_-vx</a></p>
<p>Golang documentation is just wonderful, spend more time reading it.</p></pre>buckhx: <pre><p>sync.WaitGroup is definitely the way to go here for a clean exit</p></pre>613style: <pre><p>All routines aren't deadlocked, just the main function you purposely deadlocked. By the time that message prints, the others have exited, so "all" just means the only one that's left.</p></pre>jjheiselman: <pre><p>Not sure why you're being downvoted. It's a decent question, especially for someone more familiar with the parent/child paradigm. </p></pre>
why a parent goroutine doesn't kill its child ? And why all goroutines are deadlock in this code snippet despite the return statements ?
xuanbao · · 735 次点击这是一个分享于 的资源,其中的信息可能已经有所发展或是发生改变。
入群交流(和以上内容无关):加入Go大咖交流群,或添加微信:liuxiaoyan-s 备注:入群;或加QQ群:692541889
- 请尽量让自己的回复能够对别人有帮助
- 支持 Markdown 格式, **粗体**、~~删除线~~、
`单行代码`
- 支持 @ 本站用户;支持表情(输入 : 提示),见 Emoji cheat sheet
- 图片支持拖拽、截图粘贴等方式上传