Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

select with channel <- <- channel

Tags:

select

go

channel

I am curious why the following does not work. In general select with a default: prevents deadlock, but not in this case:

package main

import "fmt"

func main () {
    a := make(chan int)
    b := make(chan int)

    select {
    case a <- <- b:
        fmt.Println("this is impossible")
    default:
        fmt.Println("select worked as naively expected")
    }
}

Obviously it doesn't like the <- <- but I'm wondering what's going on behind the surface here. In other situations <- <- is allowed (though perhaps not recommended).

like image 748
DizietEmblessSma Avatar asked Feb 24 '16 23:02

DizietEmblessSma


People also ask

How do I make a selection in a channel?

Now, a selection can be made directly in the channel itself using any of the selection tools or brushes. To finalize a selection, press Ctrl and click on the copied channel. The selected area will be represented by "marching ants". With the stripe selected, right-click inside of the selection and choose Fill.

How do I select a specific area in a channel?

To do this, click and drag the channel down to the plus ( + ) icon in the bottom-right corner. Now, a selection can be made directly in the channel itself using any of the selection tools or brushes. To finalize a selection, press Ctrl and click on the copied channel. The selected area will be represented by "marching ants".

How do I duplicate a channel in Windows 10?

Simply right click on the Channel you want to duplicate and select Duplicate. In this case I select and duplicate the Blue Channel. This duplicate channel is actually a selection. You can go to Select, Load Selection and load Blue copy.

How do I create a new channel using the base code?

We'll start from the following as a base code: The code: select_base.go The code creates a new channel with make (chan val-type). Channels are typed by the values they convey.


1 Answers

a <- <- b is the same as a<- (<-b), because the <- operator associates with the leftmost chan possible.

So the select has a case with a send operation (in the form of a<- (something)). And what happens here is that the right-hand-side expression of the send statement (the value to be sent) is evaluated first - which is <-b. But this will block forever (because no one is sending anything on b), so:

fatal error: all goroutines are asleep - deadlock!

Relevant section form the Spec: Select statements:

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. ...

So if default is present, the select does prevent blocking if none of the communications can proceed in step 2, but your code gets stuck in step 1.


Just to be complete, if there would be a goroutine that would send a value on b, then evaluation of <- b would not block, so the execution of select would not stuck in step 2, and you would see the expected "select worked as naively expected" (because receiving from a could still not proceed therefore default would be chosen):

go func() { b <- 1 }()

select {
    // ...
}

Try it on the Go Playground.

like image 170
icza Avatar answered Oct 09 '22 18:10

icza