dongsui5464 2016-02-24 23:28
浏览 52
已采纳

选择通道<-<-通道

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

  • 写回答

1条回答 默认 最新

  • dsklfsdlkf1232 2016-02-24 23:52
    关注

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 想问一下树莓派接上显示屏后出现如图所示画面,是什么问题导致的
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)
  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号