Multiple channel operation on single select case

1,656 views
Skip to first unread message

Varun Vasan V

unread,
Feb 3, 2016, 11:26:08 AM2/3/16
to golan...@googlegroups.com
Hi all. Confused with multiple channel operation on a single "select case".  
This is the program(https://play.golang.org/p/CRrQmQ1xhf)

sender1 := make(chan string)
sender2 := make(chan string)
receiver := make(chan string)

var f1 = func() {
	for {
		select {
		case receiver <- <-sender1:
		case receiver <- <-sender2:
		}
	}
}

var f2 = func() {
	for {
		select {
		case a := <-sender1:
			receiver <- a
		case b := <-sender2:
			receiver <- b
		}
	}
}

go f1() // Deadlock
//go f2()

<-receiver
<-receiver

``````

calling go f1() deadlocks, while go f2() doesn't. can anyone please help..

Chris Hines

unread,
Feb 3, 2016, 1:23:03 PM2/3/16
to golang-nuts
The language spec has this to say about evaluating a select statement:

Execution of a "select" statement proceeds in several steps:

  1. For all the cases in the statement, the channel operands of receive operations and the channel and right-hand-side expressions of send statements are evaluated exactly once, in source order, upon entering the "select" statement. The result is a set of channels to receive from or send to, and the corresponding values to send. Any side effects in that evaluation will occur irrespective of which (if any) communication operation is selected to proceed. Expressions on the left-hand side of a RecvStmt with a short variable declaration or assignment are not yet evaluated.
  2. If one or more of the communications can proceed, a single one that can proceed is chosen via a uniform pseudo-random selection. Otherwise, if there is a default case, that case is chosen. If there is no default case, the "select" statement blocks until at least one of the communications can proceed.
  3. Unless the selected case is the default case, the respective communication operation is executed.
  4. If the selected case is a RecvStmt with a short variable declaration or an assignment, the left-hand side expressions are evaluated and the received value (or values) are assigned.
  5. The statement list of the selected case is executed.
Your example code highlights a subtle point of step #1. It is important to understand that the select statement only branches on one channel expression per case. The multi channel expressions in f1() cause problems because the right most channel operation (the receives from sender1 and sender2) are evaluated sequentially upon entering the select statement. The results of those receives would provide the right hand side of the send operations that the select statement will branch on. As a result, once the sender1 channel is empty f1() will block on the first case clause without evaluating the receive from sender2. On the other hand, f2() properly waits for data on either channel concurrently by making the receive operations the focus of the select statement.

Hope that helps.
Reply all
Reply to author
Forward
0 new messages