goroutines deadlock, need some explanation

Issue

I wanted to understand how does the Go-routine work here.

Question 1:
Does the go-routine inherit anything from the main GO routine/func??

Question 2:
Also wanted to know if I send “hi whats up” into the msg channel why is it not received by the go-routine at LINE:13????

  1 package main
  2 
  3 import "fmt"
  4 
  5 func main() {
  6     msg := make(chan string, 2) //channel (bidirectional channel) of type string
  7 
  8     msg <- "hi whats up"
  9     fmt.Println("*****I am main go routine*****")
 10 
 11     go func() {
 12         fmt.Println("*****HI ABHI *****\n")
 13         temp := <-msg
 14         fmt.Println(temp)
 15         msg <- "Hello from a goroutine!" //send a string through the channel
 16     }()
 17 
 18     fmt.Println("Main routine waiting for message")
 19 
 20     fmt.Println(<-msg) //receive the string from the channel
 21     fmt.Println(<-msg)
 22 }  

Getting following error:

*****I am main go routine*****

Main routine waiting for message

hi whats up

*****HI ABHI *****

fatal error: all goroutines are asleep – deadlock!

goroutine 1 [chan receive]:

main.main()

/Users//work/go/src/github.com/Golang_play/goroutine/goroutine.go:21
+0x1e6

goroutine 5 [chan receive]:

main.main.func1(0xc420054060)

/Users/work/go/src/github.com/Golang_play/goroutine/goroutine.go:13
+0x98 created by main.main /Users/work/go/src/github.com/Golang_play/goroutine/goroutine.go:11
+0xe2 exit status 2

Also in the following program LINE 14 never prints.

 5 func main() {
  6     msg := make(chan string, 2) //channel (bidirectional channel) of type string
  7 
  8     msg <- "hi whats up"
  9     fmt.Println("*****I am main go routine*****")
 10 
 11     go func() {
 12         fmt.Println("*****HI ABHI *****\n")
 13         temp := <-msg
 14         fmt.Println(temp)
 15         //msg <- "Hello from a goroutine!" //send a string through the channel
 16     }()

Solution

You’re trying to send messages two ways on the same channel, and you don’t do anything to guarantee who reads which messages. It’s entirely possible for the main goroutine to receive the message it sent, before the func goroutine attempts to read from the channel. If that happens, both goroutines get stuck waiting for messages that will never arrive.

Use two channels.

Answered By – user2357112 supports Monica

Answer Checked By – Katrina (GoLangFix Volunteer)

Leave a Reply

Your email address will not be published.