How Go’s G-M-P Scheduler Runs Goroutines
Go’s G-M-P scheduler runs lightweight goroutines across a changing pool of operating-system threads. A G is a goroutine, an M is an OS thread, and a P is a runtime scheduling resource that allows an M to execute Go code.
The scheduler’s job is to keep goroutines moving without requiring one OS thread per goroutine, while still using available CPU cores efficiently.
The problem it solves
An OS thread is relatively expensive: it has a substantial stack, kernel-visible state, and costs time to create and switch. A server might need thousands of concurrent activities while having only a few CPU cores. If each activity required its own thread, most threads would sit idle waiting for network, disk, timers, or other work.
A goroutine is much cheaper. The Go runtime can pause one, save its execution state, and resume another on the same OS thread. But that requires a scheduler to decide which goroutine runs next, where runnable goroutines are stored, and what happens when a thread gets stuck.
The G-M-P model separates those responsibilities:
- G (goroutine): the function execution and its runtime state, including its stack.
- M (machine): an OS thread that can execute instructions.
- P (processor): a token representing permission and resources to run Go code. Each P has a local run queue.
The number of Ps is controlled by GOMAXPROCS. It defaults to a value based on the machine’s available CPUs, though a program can change it. At most that many goroutines execute Go code simultaneously, even if the runtime has more OS threads available.
How a goroutine gets to a thread
When a goroutine becomes runnable—for example, after being created, unblocked, or woken by a timer—the runtime places it on a run queue. A run queue is a collection of goroutines ready to execute.
Each P normally owns a local run queue. An M holding that P takes runnable goroutines from the queue and runs them. This local arrangement is important: choosing the next goroutine usually requires no lock shared by every thread. Less contention means the scheduler itself is less likely to become a bottleneck.
A P also has a fast-path slot called runnext, used for a goroutine that should run soon, such as one made runnable by the goroutine currently executing. The details of queue placement can vary by runtime path, but the general principle is stable: keep ordinary scheduling decisions local and cheap.
There is also a global run queue for runnable goroutines that are not currently assigned to a particular P. The scheduler checks it periodically, rather than having every scheduling decision contend on a global lock.
A goroutine does not permanently belong to an OS thread. After it yields, blocks, or is preempted, another goroutine can run on the same M and P. Likewise, a goroutine may resume on a different M later.
Work stealing keeps Ps busy
Local queues improve scalability, but they can become unbalanced. One P might have many runnable goroutines while another has none. An idle P therefore performs work stealing: it looks at another P’s local queue and takes some of its runnable goroutines.
Stealing usually moves a batch rather than a single goroutine. The idle P gets enough work to make the operation worthwhile, while the original P retains work of its own. If stealing fails, the scheduler can look at the global queue, poll for network events, or park the M until work appears.
This design avoids a central dispatcher handing out every goroutine. Most scheduling is local, while stealing provides a way to rebalance load when local decisions have produced uneven queues.
Preemption prevents monopolies
A goroutine can be runnable without voluntarily giving up the CPU. For example, a long computation might contain no channel operation, lock acquisition, sleep, or function call that naturally causes a scheduling point. Without preemption, that goroutine could delay every other goroutine assigned to the same P.
Preemption is the runtime interrupting a running goroutine so another runnable goroutine can execute. Go also has cooperative scheduling points, where the running goroutine reaches code that lets the runtime switch it out. Modern Go additionally supports asynchronous preemption: the runtime can request that a goroutine stop at a compiler-generated safe point, often using an OS signal or equivalent mechanism.
A safe point is a location where the runtime can reliably inspect and save goroutine state. Preemption is not an arbitrary corruption of the CPU’s current instruction; the runtime waits for a point where the compiler and garbage collector know how the program state is represented.
This is why scheduler behavior often appears in profiles, latency incidents, and runtime traces. A CPU-bound goroutine may be interrupted even though its code never explicitly yields. Preemption improves fairness, but it does not make CPU work free: frequent runnable goroutines still compete for the limited number of Ps.
What happens during a blocking system call
A goroutine may call into the operating system for an operation that can block, such as certain file operations. If its M simply waited while continuing to hold its P, that P could not run any other goroutine. One blocked call could therefore waste an entire scheduling slot.
Before entering a blocking system call, the runtime marks the goroutine as being in a syscall and detaches the P from that M. The goroutine remains associated with the M while the OS call waits, but the P becomes available. Another M can acquire that P and run other goroutines.
When the system call returns, the original M tries to reacquire a P. If one is available, the goroutine can continue. If not, the runtime makes the goroutine runnable—placing it on an appropriate queue—and the M waits or performs other runtime work until a P becomes available.
This handoff explains why the number of OS threads can temporarily exceed GOMAXPROCS: some Ms may be blocked in system calls while other Ms execute Go code on the available Ps.
Network I/O commonly follows a related but different path. Go’s network poller generally uses nonblocking OS operations and waits for readiness, allowing the runtime to park a goroutine while its M and P continue serving other work. The goroutine is placed back on a run queue when the socket becomes ready.
In short, Goroutines are the work, Ms are the threads, and Ps are the execution slots that regulate parallel Go code. Local queues make ordinary scheduling fast, work stealing balances load, preemption prevents CPU monopolies, and syscall handoff keeps blocked operating-system calls from stopping unrelated goroutines.