All goroutines are asleep - deadlock! ----— Error

人走茶凉 提交于 2019-12-03 17:37:27
tux21b
go Routine1(command12, response12,command13, response13 )
go Routine2(command12, response12,command23, response23) // go routine
Routine3(command12, response12,command23, response23 )

This will start Routine1 in a new goroutine, and the main goroutine will continue with the next statement. Therefore, Routine1 and Routine2 will be executed concurrently, but Routine3 will be started after Routine2 has finished. You might miss another "go" statement here.

Then, I was trying to follow your program. In Routine1 you do

command13 <- y

This will block Routine1 until there is another goroutine ready which is able to receive your message. So you need a y := <-command13 in another goroutine.

But now, lets look closely at the parameter of the other two goroutines:

Routine2(command12, response12,command23, response23)
Routine3(command12, response12,command23, response23 )

As you can see, none of the goroutines has access to command13 (but you are passing command12 twice). So, neither Routine1 nor Routine2 or Routine3 is able to continue. Deadlock!

I would recommend you to go back to the drawing board. Think about what you are trying to do first, draw some diagrams about the expected flow of messages and then, try to implement that behavior.

It's really hard to debug your program at the moment since,

  • I do not know what you are trying to do. There is no detailed description about the message flow or anything like that. In fact, your code doesn't contain any documentation at all.
  • You are passing channels which are called response23 to a parameter called response13 and so on. It's quite easy to mix them up.
  • All those generic names like command12 etc. make it hard to understand what this channel is supposed to do
  • It's a good idea to gofmt your source code before you post it :)

As a starting point, I can recommend you the "Prime Numbers" example form the Go tutorial. In this example, possible prime numbers are passed from one goroutine to another. Additionally, this example also contains some nice graphics about the message flow as well as some really good explanations. You might like it.

You have declared your channels as blocking channels. As soon as you try to do a send or receive from one of the channels the goroutine will block until that value is read or until it receives a value.

For example, in Routine1 if you call command12 <- y that goroutine will block until something else pulls y off the channel. Ditto for command13. Since you are running those sends in a loop, and Routine2 and Routine3 run synchronously you hit your deadlock problem.

Neither channels nor goroutines are a guarantee against deadlock as you have found out. Instead they are usually used to synchronize and coordinate different pieces of a concurrently executing program.

If Routine2 and Routine3 are also goroutines, then there is nothing to stop your program from terminating -- that is why you get no output in that case.

It would probably be beneficial to sit with a piece of paper and draw out the interaction between the various elements in your program.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!