Go channels and deadlock

后端 未结 3 794
情歌与酒
情歌与酒 2020-12-24 14:23

I\'m trying to understand the Go language. I tried to create two goroutines that chain the flow between them using two channels:

func main() {
c1 := make(cha         


        
3条回答
  •  一生所求
    2020-12-24 15:06

    nmichaels is right on with his answer, but I thought I'd add that there are ways to figure out where you're deadlocking when debugging a problem like this.

    A simple one is if you're on a Unix-like OS, run the command

    kill -6 [pid]
    

    This will kill the program and give a stack trace for each goroutine.

    A slightly more involved way is to attach gdb.

    gdb [executable name] [pid]
    

    You can examine the stack and variables of the active goroutine as normal, but there's no easy way to switch goroutines that I know of. You can switch OS threads in the usual way, but that might not be enough to help.

提交回复
热议问题