登录
首页 >  Golang >  Go问答

关闭 golang 通道是否会阻塞 goroutine?

来源:stackoverflow

时间:2024-02-07 20:09:46 405浏览 收藏

各位小伙伴们,大家好呀!看看今天我又给各位带来了什么文章?本文标题《关闭 golang 通道是否会阻塞 goroutine?》,很明显是关于Golang的文章哈哈哈,其中内容主要会涉及到等等,如果能帮到你,觉得很不错的话,欢迎各位多多点评和分享!

问题内容

如《Go 编程语言》一书(第 226 页)所述:

Communication over an unbuffered channel causes the sending and receiving goroutines to synchronize. Because of this, unbuffered channels are sometimes called synchronous channels. When a value is sent on an unbuffered channel, the receipt of the value happens before the reawakening of the sending goroutine.

关闭通道也会发生同样的情况吗?我的意思是,关闭通道的接收总是发生在刚刚关闭它的 goroutine 重新唤醒之前吗?

我记得读过一些与之相关的内容。我在书上搜索过,但没有找到。


正确答案


关闭通道不会像在无缓冲通道上发送那样强制 goroutine 同步。

Go 内存模型规范说:

调用 close() 的 goroutine 不会阻塞等待另一个 goroutine 接收零值。

向无缓冲通道发送值的 Goroutine 会阻塞,直到另一个 Goroutine 接收到该值。

理论要掌握,实操不能落!以上关于《关闭 golang 通道是否会阻塞 goroutine?》的详细介绍,大家都掌握了吧!如果想要继续提升自己的能力,那么就来关注golang学习网公众号吧!

声明:本文转载于:stackoverflow 如有侵犯,请联系study_golang@163.com删除
相关阅读
更多>
最新阅读
更多>
课程推荐
更多>