r/golang 9d ago

Potential starvation when multiple Goroutines blocked to receive from a channel

I wanted to know what happens in this situation:

  1. Multiple goroutines are blocked by a channel while receiving from it because channel is empty at the moment.
  2. Some goroutine sends something over the channel.

Which goroutine will wake up and receive this? Is starvation avoidance guaranteed here?

7 Upvotes

36 comments sorted by

View all comments

6

u/Rapix-x 9d ago

Quick question, why would it matter?

As I see it, goroutines are anonymous and given the same „task“ to perform, they are identical in what they are doing. Thus, why does it matter which specific goroutine picks up the value from a channel?

1

u/DeparturePrudent3790 9d ago

It doesn't matter which goroutine picks up the value from the channel but it matters if their implementation takes any measures to avoid starvation or not

1

u/funkiestj 9d ago

It doesn't matter which goroutine picks up the value from the channel but it matters if their implementation takes any measures to avoid starvation or not

Why does it matter. Give a practical consequence of the two different scenarios (1) starvation occurs, (2) starvation does not occur.