问题
I want to write three concurrent go routines that sends integers to each other. Now, my code is compiled properly, however after first execution it gives error "all goroutines are asleep - deadlock!". I tried to find the error but I could not able to find any error in code logic.Can anybody help me to find the mistake with my code. My code is given below. Thanks in advance.
package main
import "rand"
func Routine1(command12 chan int, response12 chan int, command13 chan int, response13 chan int) {
for i := 0; i < 10; i++ {
y := rand.Intn(10)
if y%2 == 0 {
command12 <- y
}
if y%2 != 0 {
command13 <- y
}
select {
case cmd1 := <-response12:
print(cmd1, " 1st\n")
case cmd2 := <-response13:
print(cmd2, " 1st\n")
}
}
close(command12)
}
func Routine2(command12 chan int, response12 chan int, command23 chan int, response23 chan int) {
for i := 0; i < 10; i++ {
select {
case x, open := <-command12:
{
if !open {
return
}
print(x, " 2nd\n")
}
case x, open := <-response23:
{
if !open {
return
}
print(x, " 2nd\n")
}
}
y := rand.Intn(10)
if y%2 == 0 {
response12 <- y
}
if y%2 != 0 {
command23 <- y
}
}
}
func Routine3(command13 chan int, response13 chan int, command23 chan int, response23 chan int) {
for i := 0; i < 10; i++ {
select {
case x, open := <-command13:
{
if !open {
return
}
print(x, " 2nd\n")
}
case x, open := <-command23:
{
if !open {
return
}
print(x, " 2nd\n")
}
}
y := rand.Intn(10)
if y%2 == 0 {
response13 <- y
}
if y%2 != 0 {
response23 <- y
}
}
}
func main() {
command12 := make(chan int)
response12 := make(chan int)
command13 := make(chan int)
response13 := make(chan int)
command23 := make(chan int)
response23 := make(chan int)
go Routine1(command12, response12, command13, response13)
go Routine2(command12, response12, command23, response23)
Routine3(command13, response13, command23, response23)
}
Can anyone inform me why if I declare Routine2 and Routine3 as go routine, why the output is [no output]. I am new in GO and as per I understood from "http://golang.org/doc/effective_go.html#concurrency", go is used for executing goroutine in parallel with other goroutines in the same address space. So, what is the problem, that all routines are running but output is [no output].
To make program more clear: What actually I am tiring to do is creating two channels between each two routines and then use one channel to send int to other channel and receive int by another channel from that routine. For example between routine 1 & 3 channels are command13 & response13. routine 1 uses command13 to send int and response13 to receive int to/from routine 3.For routine 3 response13 used to send int and command13 to receive int to/from routine 1 (command/response 13 represents channel between routine 1 and 3).Now, as three routines are concurrent and they have specific routines to handle received msg or sending msg, why they go to deadlock ?
回答1:
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 calledresponse13
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.
回答2:
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.
来源:https://stackoverflow.com/questions/8246065/all-goroutines-are-asleep-deadlock-error