is len(channel) in Go thread-safe?

huangapple go评论72阅读模式
英文:

is len(channel) in Go thread-safe?

问题

一个通道可以被多个 Go 协程同时使用。在某些 Go 协程中使用 len(channel) 来获取通道的长度是否是线程安全的?

英文:

A channel can potentially be used by multiple Go routines. Is getting the length of the channel by using len(channel) from some of the Go routines thread-safe?

答案1

得分: 5

这有点取决于你的使用情况。

虽然调用是安全的,但当其他goroutine从该通道发送/接收数据时,结果就无法被信任。

英文:

It kind of depends on your use case

it is indeed safe to call, but the result cannot be trusted when other goroutines are sending / taking from that channel

huangapple
  • 本文由 发表于 2017年2月19日 06:16:00
  • 转载请务必保留本文链接:https://go.coder-hub.com/42321309.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定