Can I rely on the order of select wake ups based on sending order?

Issue

Imagine a code pattern like the following:

var a chan T = make(chan T)
var b chan Q = make(chan Q)

//consumer goroutine
for {
   select {
   case <-a:
       //...
   case <-b:
       //...
   }
}

//producer goroutine
...
a <- x
b <- y
...

Is it guaranteed that we can always retrieve x from case: <-a BEFORE y from case :<-b provided that there is only one producer?

Solution

Yes, you can rely on the order because channel a is unbuffered.

Communication on an unbuffered channel succeeds only when the sender and receiver are ready. The sender does not execute past the statement a <- x until the value was received. It follows from this that the values are received in send order.

The order is not guaranteed if channel a is buffered. The receiver might not execute until both values are sent. When the receiver does execute after the values are sent, there’s no guarantee on which branch of the select will execute first.

Answered By – Bayta Darell

Answer Checked By – Dawn Plyler (GoLangFix Volunteer)

Leave a Reply

Your email address will not be published.