I'd like to know the implementation reason for this:
package main
func main() {
c := make(chan struct{})
go func() {
print("a")
for {
}
}()
go func() {
print("b")
for {
}
}()
go func() {
print("c")
c <- struct{}{}
for {
}
}()
<-c
}
❯❯❯ GOMAXPROCS=2 go run sample1.go
ab <--- blocks.
package main
// static void loop() { for(;;); }
import "C"
func main() {
c := make(chan struct{})
go func() {
print("a")
C.loop()
print("x")
}()
go func() {
print("b")
C.loop()
print("y")
}()
go func() {
print("c")
c <- struct{}{}
C.loop()
print("z")
}()
<-c
}
❯❯❯ GOMAXPROCS=2 go run sample2.go
abc <--- ends gracefully.
More specifically I mean how the C tight loop differs from the Go one in the context of go routine scheduling. And even though the C tight loops should be being abruptly terminated at the end of the Go program, I'd like to know whether it's safe to rely on this behavior to start C tasks without blocking the Go program.