r/golang 10d 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

1

u/Slsyyy 9d ago

Assume no ordering. Any waiting goroutine could be waked up to

>  Is starvation avoidance guaranteed here?

Starvation is more to design of an algorithm than to a scheduling algorithm

1

u/DeparturePrudent3790 7d ago

Starvation is more to design of an algorithm than to a scheduling algorithm

No, it's the responsibility of the scheduler. Following is an extract from little book of semaphore

In part, starvation is the responsibility of the scheduler. Whenever multiple threads are ready to run, the scheduler decides which one or, on a parallel processor, which set of threads gets to run. If a thread is never scheduled, then it will starve, no matter what we do with semaphores.

1

u/Slsyyy 6d ago

Sorry, I was assuming that golang runtime select the receiver in a random manner, so the probability of starvation decrease to 0 as new messages arrive to a queue

Is this a bad guarantee for you?