GMP — Goroutine (G), Machine (M), Processor (P). Go's scheduler architecture that efficiently maps lightweight goroutines onto OS threads.
Component roles:
GOMAXPROCS. Mediates between G and M.Scheduling flow:
1// Check GOMAXPROCS setting2fmt.Println("CPUs:", runtime.NumCPU())3fmt.Println("GOMAXPROCS:", runtime.GOMAXPROCS(0))45// Runtime scheduler control6runtime.Gosched() // Yield to scheduler7runtime.LockOSThread() // Pin to current OS thread8runtime.NumGoroutine() // Count active goroutines
Work stealing algorithm:
Handoff mechanism: When M enters a blocking syscall:
When GMP knowledge helps:
Common mistakes:
runtime.GOMAXPROCS(0) (returns current) with setting a value.runtime.LockOSThread() without need (reduces flexibility).