GMP — Goroutine (G), Machine (M), Processor (P). Go's scheduler architecture that enables efficient concurrency by multiplexing goroutines onto OS threads.
Component details:
_Gidle, _Grunnable, _Grunning, _Gsyscall, _Gwaiting.GOMAXPROCS (default: runtime.NumCPU()).Scheduling algorithm:
1// Check runtime configuration2fmt.Println("GOMAXPROCS:", runtime.GOMAXPROCS(0))3fmt.Println("NumCPU:", runtime.NumCPU())4fmt.Println("NumGoroutine:", runtime.NumGoroutine())56// Scheduler hints7runtime.Gosched() // Yield to scheduler8runtime.Goexit() // Exit current goroutine
Work stealing process:
Handoff (syscall handling):
When GMP knowledge helps:
Common mistakes:
GOMAXPROCS(0) (read) with GOMAXPROCS(n) (set).LockOSThread() without need.