Go uses an M:N scheduling model where M goroutines run on N OS threads. Understand the GMP model (Goroutine, Machine, Processor) and see how work stealing keeps all processors busy.
The GMP model
G (Goroutine) is a unit of work. M (Machine) is an OS thread. P (Processor) is a context that runs goroutines on a machine. Each P has a local run queue; global queue handles overflow.
Work stealing
When a P local queue is empty, it steals half the work from another P queue. This balances load across processors and maximizes CPU utilization.
Good for
- Understanding Go concurrency
- Optimizing Go applications
- Learning runtime internals
- Debugging performance issues
Questions people ask
How many threads does a Go program use?
GOMAXPROCS sets the number of Ps (logical processors). The runtime may create more Ms (threads) for blocking operations like syscalls or CGO calls.
What triggers goroutine scheduling?
Function calls, channel operations, system calls, and explicit yields (runtime.Gosched) are scheduling points where the runtime may switch goroutines.