concurrency - Reusing a Go channel causes deadlock -
i'm new golang (whith java concurrency background). consider peace of code :
package main import "fmt" func sendenum(num int, c chan int) { c <- num } func main() { c := make(chan int) go sendenum(0, c) x, y := <-c, <-c fmt.println(x, y) } when run code , error
fatal error: goroutines asleep - deadlock! goroutine 1 [chan receive]: main.main() /home/tarrsalah/src/go/src/github.com/tarrsalah/stackoverflow/chan_dead_lock.go:12 +0x90 exit status 2 i know, adding go sendenum(0, c) statement fix issue, ... but
when , where deadlock happened ?
after receives 0, main keeps on waiting on receiving end of c value arrive (to put in y variable), never will, goroutine running main 1 left live.
when add go sendenum(0, c), gets value on second channel receive, puts y variable, prints x , y out , program finishes succesfully.
Comments
Post a Comment