求解关于TheWayToGo中描述协程未完成写入通道时,如果主程序结束,该协程不会被垃圾回收的章节

Groza · · 2345 次点击 · 开始浏览    置顶
这是一个创建于 的主题,其中的信息可能已经有所发展或是发生改变。

"The Way to Go" ---- page 395 > The code which calls this method can then iterate over the container like: ```go for x := range container.Iter() { ... } ``` which can run in its own goroutine, so then the above iterator employs a channel and two goroutines (which may run in separate threads). Then we have a typical producer-consumer pattern. If the program terminates before the goroutine is done writing values to the channel, then that goroutine will not be garbage collected; this is by design(这是指Golang的design还是上面那段代码的design?). This seems like wrong behavior, but channels are for threadsafe communication(不太明白这种行为和和线程安全有什么关系). In that context, a goroutine hung trying to write to a channel that nobody will ever read from is probably a bug and not something you’d like to be silently garbage-collected.(因为上面那个design到底指的谁我不太明白,所以这个地方到底是想表达“这看起来像bug(如果之前指的是golang的design)”,还是像表达“这样就是一个bug”(如果指之前的代码的design),不要这样写) 对这一段的疑问我都在上面的括号里写出来了,还望解惑,谢谢各位大佬!

有疑问加站长微信联系(非本文作者)

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

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