[Go - Note] Channel 阻塞deadlock和panic情况,以及close channel

栏目: IT技术 · 发布时间: 5年前

内容简介:发生一直阻塞时,会报如下deadlock错误:1.向已经关闭的channel写。2.关闭已经关闭的channel。

阻塞:

发生一直阻塞时,会报如下deadlock错误:

fatal error: all goroutines are asleep - deadlock!

无缓存channel:

  1. 通道中无数据,但执行读通道。
  2. 通道中无数据,向通道写数据,但无协程读取。

有缓存channel:

  1. 通道的缓存无数据,但执行读通道。
  2. 通道的缓存已经占满,向通道写数据,但无协程读。

Panic:

1.向已经关闭的channel写。

2.关闭已经关闭的channel。

close channel:

You needn't close every channel when you've finished with it. It's only necessary to close a channel when it is important to tell the receiving goroutines that all data have been sent.

Note that it is only necessary to close a channel if the receiver is looking for a close. Closing the channel is a control signal on the channel indicating that no more data follows.

It's OK to leave a Go channel open forever and never close it. When the channel is no longer used, it will be garbage collected.


以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,也希望大家多多支持 码农网

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

Algorithms of the Intelligent Web

Algorithms of the Intelligent Web

Haralambos Marmanis、Dmitry Babenko / Manning Publications / 2009-7-8 / GBP 28.99

Web 2.0 applications provide a rich user experience, but the parts you can't see are just as important-and impressive. They use powerful techniques to process information intelligently and offer featu......一起来看看 《Algorithms of the Intelligent Web》 这本书的介绍吧!

在线进制转换器
在线进制转换器

各进制数互转换器

MD5 加密
MD5 加密

MD5 加密工具

SHA 加密
SHA 加密

SHA 加密工具