why a parent goroutine doesn't kill its child ? And why all goroutines are deadlock in this code snippet despite the return statements ?

xuanbao · · 726 次点击    
这是一个分享于 的资源,其中的信息可能已经有所发展或是发生改变。
<pre><code>package main import ( &#34;fmt&#34; ) func launchRoutine(i int) { go func(i int) { fmt.Printf(&#34;goroutine : %d\n&#34;, i) go func(i int) { fmt.Printf(&#34;subroutine : %d\n&#34;, i) return }(i) return }(i) return } func main() { for i := 0; i &lt; 10; i++ { go launchRoutine(i) } &lt;-make(chan int) //holds main goroutine forever } </code></pre> <hr/>**评论:**<br/><br/>enneff: <pre><p>Goroutines don&#39;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&#39;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>&lt;-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>&lt;-make(chan int)</strong> by <strong>select {}</strong> and it will work.</p></pre>infogulch: <pre><blockquote> <p>Replace &lt;-make(chan int) by select {} and it will work.</p> </blockquote> <p>This doesn&#39;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&#39;t deadlocked, just the main function you purposely deadlocked. By the time that message prints, the others have exited, so &#34;all&#34; just means the only one that&#39;s left.</p></pre>jjheiselman: <pre><p>Not sure why you&#39;re being downvoted. It&#39;s a decent question, especially for someone more familiar with the parent/child paradigm. </p></pre>

入群交流(和以上内容无关):加入Go大咖交流群,或添加微信:liuxiaoyan-s 备注:入群;或加QQ群:692541889

726 次点击  
加入收藏 微博
暂无回复
添加一条新回复 (您需要 登录 后才能回复 没有账号 ?)
  • 请尽量让自己的回复能够对别人有帮助
  • 支持 Markdown 格式, **粗体**、~~删除线~~、`单行代码`
  • 支持 @ 本站用户;支持表情(输入 : 提示),见 Emoji cheat sheet
  • 图片支持拖拽、截图粘贴等方式上传