go - 单个 channel 上的多个接收器。谁得到数据?

标签 go blocking channel

无缓冲 channel 会阻塞接收器,直到 channel 上有数据可用。我不清楚这种阻塞如何与同一 channel 上的多个接收器(例如在使用 goroutines 时)一起表现。我敢肯定,只要该 channel 上没有发送数据,它们就会全部阻塞。
但是,一旦我向该 channel 发送一个值,会发生什么?哪个接收器/goroutine 将获取数据并因此解除阻塞?他们全部?排第一?随机?

最佳答案

一个随机(非确定性)的人会收到它。

查看语言 spec :

Execution of a "select" statement proceeds in several steps:

  1. For all the cases in the statement, the channel operands of receive operations and the channel and right-hand-side expressions of send statements are evaluated exactly once, in source order, upon entering the "select" statement. The result is a set of channels to receive from or send to, and the corresponding values to send. Any side effects in that evaluation will occur irrespective of which (if any) communication operation is selected to proceed. Expressions on the left-hand side of a RecvStmt with a short variable declaration or assignment are not yet evaluated.
  2. If one or more of the communications can proceed, a single one that can proceed is chosen via a uniform pseudo-random selection. Otherwise, if there is a default case, that case is chosen. If there is no default case, the "select" statement blocks until at least one of the communications can proceed.
  3. Unless the selected case is the default case, the respective communication operation is executed.
  4. If the selected case is a RecvStmt with a short variable declaration or an assignment, the left-hand side expressions are evaluated and the received value (or values) are assigned.
  5. The statement list of the selected case is executed.

关于go - 单个 channel 上的多个接收器。谁得到数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31539804/

相关文章:

struct - 嵌入式结构方法可以了解父/子吗?

Python2.6.5 : Is there python equivalent of Java Semaphore tryAcquire

java - 奇怪的线程 block 创建原始数组

tcp - 简单的 Rust TCP 服务器和客户端不接收消息并且永不终止

go - 为什么我的 client.go 无法访问 math.go?

go - 使用go mod时出现 'ambiguous import'怎么解决?

hadoop - HDFS 排除 AddblockRequestProto 中的数据节点

go - 同时计算树叶

go - 惯用的 goroutine 终止和错误处理

concurrency - 使用 Go 通过 channel 发送 channel