dongshan1396 2019-07-21 14:39
浏览 5
已采纳

在go例程之外向通道发送值时,go例程陷入僵局

I tried changing a little bit on the code when learning Go select statement in Golang tour: https://tour.golang.org/concurrency/5. However, i got issue:

fatal error: all goroutines are asleep - deadlock!

goroutine 1 [chan send]:
main.main()
        concurrency.go:26 +0xa3

goroutine 33 [chan receive]:
main.main.func1(0xc000088000)
        concurrency.go:24 +0x42
created by main.main
        concurrency.go:23 +0x89
exit status 2

Here is the code i tried and got the issue

func fibonacci(c, quit chan int) {
    x, y := 0, 1
    for {
        select {
            case c <- x:  //sending value x into channel c
                x, y = y, x+y
            case <-quit:    //receive value from quit
                fmt.Println("quit")
        return
        }
    }
}

func main() {
    //create two channels
    c := make(chan int)
    quit := make(chan int)
    go func() { //spin off the second function in order to let consume from c , so fibonaci can continue to work
        fmt.Println(<-c)    //read value from channel c
    }()
    //Try moving the statement that send value to channel quit in order to 
    //return function fibonacci
    quit <- 0   
    fibonacci(c, quit)
}

At first, i thought that the result will be same with result of below code

//function fibonacci is same with the first one
func fibonacci(c, quit chan int) {
    x, y := 0, 1
    for {
        select {
            case c <- x:  //sending value x into channel c
                x, y = y, x+y
            case <-quit:    //receive value from quit
                fmt.Println("quit")
        return
        }
    }
}

func main() {
    //create two channels
    c := make(chan int)
    quit := make(chan int)
    go func() { //spin off the second function in order to let consume from c , so fibonaci can continue to work
        fmt.Println(<-c)    //read value from channel c
        quit <- 0 //CHANGE: move the statement inside the closure function 
    }()

    fibonacci(c, quit)
}

The output is

0
quit

Can you please explain what's the root cause of deadlock when executing the first example? And what are differences when sending value to quit channel in the go routines with sending value to quit channel in the main thread.

Thank you guys.

  • 写回答

1条回答 默认 最新

  • dongshan7708 2019-07-21 14:50
    关注

    The quit channel is an unbuffered channel. Communication on an unbuffered channel does not proceed until both a sending and receiving goroutine are ready. The statement quit <- 0 blocks before the application executes the function to receive the value. A receiving goroutine will never be ready

    Fix by closing the channel:

    c := make(chan int)
    quit := make(chan int)
    go func() {
        fmt.Println(<-c)
    }()
    close(quit)
    fibonacci(c, quit)
    

    ... or by making the channel buffered

    c := make(chan int, 1) // <- note size 1
    quit := make(chan int)
    go func() { 
        fmt.Println(<-c) 
    }()
    quit <- 0   
    fibonacci(c, quit)
    

    In this scenario, fibonacci will quit before yielding a value.

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

报告相同问题?

悬赏问题

  • ¥15 做个有关计算的小程序
  • ¥15 MPI读取tif文件无法正常给各进程分配路径
  • ¥15 如何用MATLAB实现以下三个公式(有相互嵌套)
  • ¥30 关于#算法#的问题:运用EViews第九版本进行一系列计量经济学的时间数列数据回归分析预测问题 求各位帮我解答一下
  • ¥15 setInterval 页面闪烁,怎么解决
  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化