In a version prior to the release of go 1.5 of the Tour of Go website, there's a piece of code that looks like this.
package main
import (
"fmt"
"runtime"
)
func say(s string) {
for i := 0; i < 5; i++ {
runtime.Gosched()
fmt.Println(s)
}
}
func main() {
go say("world")
say("hello")
}
The output looks like this:
hello
world
hello
world
hello
world
hello
world
hello
What is bothering me is that when runtime.Gosched()
is removed, the program no longer prints "world".
hello
hello
hello
hello
hello
Why is that so? How does runtime.Gosched()
affect the execution?
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…