具有无缓冲通道的Golang例程
I am reading the book Go in action
.
This is how the unbuffered channel
are described:
An unbuffered channel is a channel with no capacity to hold any value before it’s received. These types of channels require both a sending and receiving goroutine to be ready at the same instant before any send or receive operation can complete. If the two goroutines aren’t ready at the same instant, the channel makes the goroutine that performs its respective send or receive operation first wait. Synchronization is inherent in the interaction between the send and receive on the channel. One can’t happenwithout the other.
The book use the following figure to illustrate the unbuffered channel
:
So I just wonder how about if there are three or more goroutine
which share the same channel
?
For example, three goroutine GA
GB
GC
share the same channel c
Now once GA
send message through c
, how do we make sure that both GB
and GC
will receive the message? Since as the book said:
These types of channels require both a sending and receiving goroutine to be ready at the same instant
Which means when two goroutine are exchanging the message, the third must lost the message.
Is this the right way goroutine run?
我正在读这本书 这是 无缓冲通道是没有能力保存任何值的通道
在收到之前。 在完成任何发送或接收操作之前,这些类型的通道需要同时准备好发送和接收goroutine。 如果两个goroutine尚未同时准备就绪,则通道将首先等待执行各自发送或接收操作的goroutine。
同步是发送和接收消息之间固有的同步性。 渠道。 p>
blockquote>
这本书使用下图说明 所以我只想知道是否存在三个或更多共享的 例如,三个goroutine 现在,一旦 这些类型的通道都需要发送和接收goroutine
同时准备就绪 p>
blockquote>
这意味着当两个goroutine交换消息时,第三个必须丢失消息。 p>
这是goroutine运行的正确方法吗? p> \ n div>行动起来 code>。 p>
非缓冲通道 code>的描述方式: p>
无缓冲通道 code>: p>
goroutine code> 相同的
channel code>? p>
GA code>
GB code>
GC code>共享同一通道
c code> p>
GA code>通过
c code>发送消息,我们如何确保两个
GB code> 和
GC code>会收到消息吗? 正如书中所述: p>
A message sent through a channel is delivered to exactly one receiving goroutine.
Assuming you have three goroutines, one sending and two receiving, the sending goroutine needs to send two messages for both the receiving goroutines to unblock, like this:
var c = make(chan int)
go func() { fmt.Printf("got %d
", <-c) }()
go func() { fmt.Printf("got %d
", <-c) }()
c <- 1
c <- 2
Also notice that this also requires the receiving goroutines to read exactly one message each. If they were to do it in a loop, one of them might receive both messages and the other none.
If there are 2 or many receivers and one sender on a channel(buffered/unbuffered) and sender sends a message through the channel, only one out of all the receivers will get the message.
You are right, only one receiver can get one specific message from a channel (buffered or unbuffered). If you want multiple receivers to read all messages from a channel, you will have to put a multiplexer between the original channel and the receivers, which would read the messages and forward them to N
channels, one for each receiver.
If we would draw it out it would look something like this (GA
is the sender, GB
, GC
, GZ
are receivers, c
, cB
, cC
, cZ
are channels and multiplexer
is self explanatory).
+-> [cB] -> GB
|
GA -> [c] -> multiplexer +-> [cC] -> GC
|
...
|
+-> [cZ] -> GZ
You can also look at this answer for a code example.
Let say goroutine A and B are reading from unbuffered channel c
. They are both blocked as the channel is unbuffered.
Goroutine D write something into channel. One of A or B will receive data and will be unblocked the other one stay blocked until D or other goroutine write another thing into channel.