本文介绍了单个通道上有多个接收器。谁获取数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

未缓冲的通道阻塞接收器,直到通道上的数据可用。我不清楚这种阻塞在同一个信道上有多个接收器的行为(比如说使用goroutines时)。我相信,只要在该频道上没有数据发送,它们就会全部阻止。

但是,一旦我将单个值发送到该频道,会发生什么?哪个接收器/ goroutine会得到数据并因此解除阻塞?他们全部?第一个在线?随机?

解决方案

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



查看语言:


Unbuffered channels block receivers until data is available on the channel. It's not clear to me how this blocking behaves with multiple receivers on the same channel (say when using goroutines). I am sure they would all block as long as there is no data sent on that channel.
But what happens once I send a single value to that channel? Which receiver/goroutine will get the data and therefore unblock? All of them? The first in line? Random?

解决方案

A single random (non-deterministic) one will receive it.

See the language spec:

这篇关于单个通道上有多个接收器。谁获取数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-06 03:17