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

weixin_38087646 2019-09-20 10:40:55
"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),不要这样写)


对这一段的疑问我都在上面的括号里写出来了,还望解惑,谢谢各位大佬!
...全文
46 回复 打赏 收藏 转发到动态 举报
写回复
用AI写文章
回复
切换为时间正序
请发表友善的回复…
发表回复

433

社区成员

发帖
与我相关
我的任务
社区描述
其他技术讨论专区
其他 技术论坛(原bbs)
社区管理员
  • 其他技术讨论专区社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧