all go routines are asleep - deadlock

前端 未结 1 1170
醉话见心
醉话见心 2020-12-07 04:29

I don\'t understand why the deadlock is occurring in this code. I\'ve tried several different things to get the deadlock to stop (several different versions using WorkGroup)

相关标签:
1条回答
  • 2020-12-07 05:04

    You never close the "truck" channel ch, so UnloadTrucks never returns.

    You can close the channel after all workers are done, by using a WaitGroup:

    go func() {
        wg.Wait()
        close(ch)
    }()
    UnloadTrucks(ch)
    

    http://play.golang.org/p/1V7UbYpsQr

    0 讨论(0)
提交回复
热议问题