runtime.Gosched yields the processor so another runnable goroutine can run. It invokes the Go scheduler from the current goroutine, but does not suspend it permanently. section 2.3.3 section 3.3.2
Use runtime.Gosched with these limitations in mind:
- Calling it gives another goroutine a chance to execute before the current goroutine continues. section 2.3.3
- It provides no guarantee about which goroutine the scheduler selects or whether another goroutine runs at all. section 2.3.3
- It does not make unsynchronized shared-memory updates safe and must not be used to fix race conditions. section 3.3.2
Giving a greeting goroutine a chance
Starting sayHello in a goroutine and then calling runtime.Gosched before printing Finished increases the chance that Hello appears. The program can still finish without printing Hello because main may continue or terminate first. section 2.3.3
Runnable listing showing runtime.Gosched called after starting sayHello. section 2.3.3
