duanpu1064
duanpu1064
2018-08-30 23:16
浏览 139
已采纳

为什么我的代码会死锁以及如何解决它

Why does my code deadlock (followed by crash) on read from channel, I would expect it to block on read after the channel is read completely, but not crash. I understand it is a deadlock state as no one is writing to the channel and read blocks on it.

How can I change the code to read all the channel content and then exit from main instead of crash.

Go playground: https://play.golang.org/p/rjXZZOx1FFZ

package main

import (
    "fmt"
    "sync"
)

func main() {
    var wg sync.WaitGroup
    news := make(chan int, 10)

    wg.Add(1)

    go foo(&wg, news) 

    wg.Wait()   

    for {
        fmt.Printf("reading: %v
", <-news) 
        //crashes here after printing 0-9
    }

}

func foo(wg *sync.WaitGroup, news chan int) {
    for i:=0; i<10;i++ {
        fmt.Printf("Writing
")
        news <- i
    }
    (*wg).Done()
}
  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

3条回答 默认 最新

  • doudun3040
    doudun3040 2018-08-30 23:28
    已采纳

    The program deadlocks because main blocks on channel receive and there are no other goroutines that will send to that channel.

    Use this approach to read all the channel content and then exit from main: main reads the channel until closed; foo writes all values and closes the channel.

    func main() {
        news := make(chan int, 10)
        go foo(news)
        // range breaks when the channel is closed
        for v := range news { 
    
            fmt.Printf("reading: %v
    ", v)
        }
    
    }
    
    func foo(news chan int) {
        for i := 0; i < 10; i++ {
            fmt.Printf("Writing
    ")
            news <- i
        }
        // close channel to indicate that no more values will be sent.
        close(news) 
    }
    

    Run it on the Playground.

    点赞 评论
  • dousi4472
    dousi4472 2018-08-30 23:41

    For an explanation of why this happens, the error message you get from the panic tells you the gist of it:

    fatal error: all goroutines are asleep - deadlock!
    

    You cannot have all of your goroutines waiting for other goroutines to do something. In this case, when the goroutine running foo is done, and the goroutine running main has received all of the messages (0 to 9) that were sent to the news channel, your program is left with one goroutine waiting to receive a message on a channel that will never have a message sent to it again because there are no other goroutines around to do it.

    点赞 评论
  • dongyao4419
    dongyao4419 2018-08-30 23:55

    The code crashes due to deadlock.

    fatal error: all goroutines are asleep - deadlock!

    You have a couple interesting problems with the original code.

    If you close the news channel in your foo goroutine instead of deadlocking you instead will just run your infinite loop in main forever.

    2 simple code changes should make this a bit more clear and will cause it to exit after processing the news items.

    https://play.golang.org/p/qSU7sV7Wrov

    package main
    
    import (
        "fmt"
        "sync"
    )
    
    func main() {
        var wg sync.WaitGroup
        news := make(chan int, 10)
    
        wg.Add(1)
    
        go foo(&wg, news)
    
        wg.Wait()
    
        for n := range news {
            fmt.Printf("reading: %v
    ", n)
        }
        fmt.Println("We're out of news!")
    }
    
    func foo(wg *sync.WaitGroup, news chan int) {
        for i := 0; i < 10; i++ {
            fmt.Printf("Writing
    ")
            news <- i
        }
        close(news)
        wg.Done()
    }
    

    Of course, you can rewrite it without using WaitGroups and just use the channels as others have pointed out and that may be a more elegant solution.

    点赞 评论

相关推荐