You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
package main
import (
"fmt"
)
func foo(ch chan string) {
defer func() {
ch <- "finish"
}()
// do some process
// ...
}
func main() {
ch := make(chan string)
for i := 0; i <= 100; i++ {
ch <- "start"
go foo(ch)
}
var (
msg string
allroutine int
finished int
)
// In this loop, I record two flags of goroutines.
// If a routine start, a "start" message will emit
// to the channel.
// If a routine finish, a "finish" message will emit
// to the channel.
// The variable allroutine always bigger than variable
// finished because routine first start and finish
// later.
// If finished equal to the allroutine, it means
// that all routines finished already.
for {
msg = <-ch
if msg == "start" {
allroutine++
}
if msg == "finish" {
finished++
}
fmt.Println("allroutine:", allroutine, "finished:", finished)
if finished == allroutine {
break
}
}
}
What does 'go version' print?
go v1.2.1
What happened?
deadlock...
What should have happened instead?
It will finished with the output "allroutine: 100 finished: 100".
Please provide any additional information below.
The text was updated successfully, but these errors were encountered:
by jikai507:
The text was updated successfully, but these errors were encountered: