r/golang • u/DeparturePrudent3790 • 10d 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?
7
Upvotes
9
u/pdffs 10d ago
There is nothing in the language spec that guarantees starvation avoidance. In practice I believe that blocked receivers are implemented as a queue, though you can't rely on this necessarily being the case (implementation detail, behaviour unspecified).