For the below code:
package main
import (
"fmt"
"syscall"
)
func block(c chan bool) {
fmt.Println("block() enter")
buf := make([]byte, 1024)
_, _ = syscall.Read(0, buf) // block on doing an unbuffered read on STDIN
fmt.Println("block() exit")
c <- true // main() we're done
}
func main() {
c := make(chan bool)
for i := 0; i < 1000; i++ {
go block(c)
}
for i := 0; i < 1000; i++ {
<-c
}
}
Ubuntu 12.04 reported 1004 OS threads for that process.
If Goroutine(G1
) blocks(for syscall.Read()
) on OS thread(M1
), as shown below:
$ go version
go version go1.20.2 linux/amd64
How does Goroutine(G1
) complete the blocking system call(syscall.Read()
) on OS thread(M1
), before moving back to LRQ? Doesn’t OS thread(M1
) need CPU core to complete blocking system call?