package main
import (
"fmt"
"time"
"runtime"
)
var quit chan int = make(chan int)
func loop(a int) {
fmt.Println(a)
for i := 0; i < 30000000000; i++ {
}
fmt.Println(a)
quit <- 0
}
func main() {
runtime.GOMAXPROCS(1)
go loop(1)
time.Sleep(time.Second)
go loop(2)
for i := 0; i < 2; i++ {
<-quit
}
}
For the scheduler model (M+P+G), I suppose we just have 1 cpu context because we set GOMAXPROCS
as 1, and there is just 1 thread(M) here.
In the goroutine, the for loop do not has any IO blocking, so no new thread will be generated, all goroutine should still work in current thread, so I think the 2 goroutine must go one by one, thus, the result should be 1 1 2 2
. But in fact, the result is 1 2 1 2
. Why?