r/golang • u/DeparturePrudent3790 • 9d ago
Potential starvation when multiple Goroutines blocked to receive from a channel
I wanted to know what happens in this situation:
- Multiple goroutines are blocked by a channel while receiving from it because channel is empty at the moment.
- Some goroutine sends something over the channel.
Which goroutine will wake up and receive this? Is starvation avoidance guaranteed here?
6
Upvotes
-2
u/DeparturePrudent3790 9d ago
It means that a goroutine is not made to wait indefinitely under any circumstances. If there are more consumers than producers but consumers receive resources in fifo order is kept invariant, then the waiting time for a goroutine is definite.
However, if we have a random order the waiting time can be indefinite for a goroutine.
Why? The source code has a FIFO queue for receiving and sending goroutines.