Open Vogel-Guo opened 5 years ago
You can read from a closed channel as much as you like, it returns a infinite number of null-values of the channel-type (int 0 here).
So, indeed walkImpl
is still called for the "leftmost" branch of the current node in the tree (down to the leaf) but all of those calls will successfully read a value from quit
and return.
solution with quit for Exercise: Equivalent Binary Trees Supposed we have two simple equivalent binary trees "1 3 5" and "2 3 5". When two goroutines "Walk" walks at leaf "1" and "2" concurrently,
this condition in function Same will be true and
close(quit)
will run.
Channel "quit" will receive message and the second case of select statement will execute. And then it will go back to the upper level function "walkImpl" and keep on running the last line
walkImpl(t.Right, ch, quit)
. So is there goroutine leak under the circumstance, cause channel "quit" is already read out, which can't be read again in the upper level? Function "Walk" also can't go back to "close" handler.