Reputation: 36028
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 happen without 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 goroutines which share the same channel?
For example, three goroutines 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 goroutines exchange the message, the third must've lost the message.
Is this the right way to think in this scenario?
Upvotes: 0
Views: 1824
Reputation: 1635
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\n", <-c) }()
go func() { fmt.Printf("got %d\n", <-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.
Upvotes: 4
Reputation: 1254
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.
Upvotes: 1
Reputation: 6536
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.
Upvotes: 0
Reputation: 1151
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.
Upvotes: 0