How the Go Scheduler Works (GMP Model)

The Go scheduler uses the GMP model to efficiently run millions of goroutines on limited OS threads by dynamically balancing work and handling blocking operations.

The Go scheduler uses the GMP model (Goroutine, Machine, Processor) to multiplex millions of goroutines across a limited number of OS threads. A Processor (P) holds a local queue of Goroutines (G) and runs them on a Machine (M), which is an OS thread; if a P's queue is empty, it steals work from other Ps or parks the M until work arrives. This design allows the runtime to balance load dynamically and handle blocking system calls by migrating Ps to different Ms without stopping the program.

// Conceptual representation of the GMP relationship
// M (OS Thread) executes G (Goroutine) using P (Processor) as the context
// P holds the local run queue and the current G
// M runs the P's current G until it blocks or finishes

// Example: When a goroutine blocks on I/O, the M is released
// The P is moved to another M to continue executing other Gs
runtime.Gosched() // Yields the processor to the scheduler