The Go scheduler multiplexes many goroutines onto a small number of operating system threads using the GMP model: G is a goroutine, M is a machine (an OS thread) and P is a processor, a token that holds the right to run Go code. An M needs a P to execute a G, and the number of Ps equals GOMAXPROCS. This is what lets one program run hundreds of thousands of goroutines cheaply.
This guide explains each part of the go scheduler gmp design, how work moves between run queues, what happens on blocking calls, and how preemption works.
| Part | Meaning | Holds |
|---|---|---|
| G | goroutine | its stack (starts small and grows), program counter, status |
| M | OS thread | the thread that executes code, tied to a P while running Go code |
| P | logical processor | a local run queue of runnable Gs and per-P caches |
The number of Ps is set by GOMAXPROCS. Historically it defaulted to the number of logical CPUs. From Go 1.25 the default also takes the container CPU limit (cgroup quota) into account, so a service limited to 2 CPUs on a large host no longer starts dozens of Ps. You can read or change it with runtime.GOMAXPROCS.
The count of Ms is not fixed. The runtime creates threads as needed, for example when goroutines sit in blocking system calls, up to a large limit (debug.SetMaxThreads, 10000 by default).
When you write go f(), the runtime creates a G and puts it in the local run queue of the current P. That queue is a fixed-size ring of 256 entries. When it is full, half of its Gs move to the global run queue, which is shared and protected by a lock.
Each M that holds a P runs a loop, called schedule, that picks the next G in this order:
runnext slot, which holds the goroutine that was just made runnable by the current one (this keeps a producer and consumer close).When a P has nothing to run, its M looks at other Ps and steals about half of the Gs from a victim's local queue. This balances load without a central lock on the fast path. If nothing can be stolen, the M parks and the P goes idle until new work arrives.
This design is why spawning goroutines is cheap: creating one is a queue push, and moving between goroutines is a user-space context switch of a few registers, not an OS thread switch.
Network I/O does not block threads. Sockets are non-blocking and registered with the netpoller (epoll on Linux, kqueue on macOS and BSD). A goroutine waiting on a read is parked, its M and P move on, and the netpoller makes the G runnable when data arrives.
Blocking calls that cannot use the poller, such as many file operations or cgo calls, are handled by handoff:
sysmon, notices the P has been in a syscall too long and hands it to another M (creating one if needed), so the other Gs keep running.This is why many goroutines in blocking file or cgo calls can lead to many OS threads.
Go scheduling is cooperative with a safety net. Goroutines yield at channel operations, locks, time.Sleep, function calls (stack check points) and runtime.Gosched(). A tight loop with no calls once could block others forever.
Since Go 1.14 the runtime has asynchronous preemption: sysmon notices a G has run for more than 10 ms and sends the thread a signal, and the signal handler stops the goroutine at a safe point so another can run. The time slice is therefore about 10 ms, not a precise guarantee.
The GODEBUG=schedtrace option prints one summary line per interval.
idleprocs shows how many Ps are idle, runqueue the global queue length, and the bracketed list the local queue length of each P. For per-goroutine detail use go tool trace, and for CPU use pprof.
GOMAXPROCS higher than your CPU quota. Extra Ps only add contention and throttling.runtime.LockOSThread only when a library requires thread affinity, as it removes the freedom of the scheduler.It limits the number of Ps, so the number of threads executing Go code at the same time. Threads blocked in system calls do not count against it.
It starts at a few kilobytes and grows and shrinks automatically as the function needs it, which is why you can run very many goroutines.
No. Many goroutines share a few threads (M:N scheduling), and the scheduler decides which goroutine runs on which thread.
No. The order is not defined, so never rely on a goroutine starting or finishing before another without synchronisation.
In the GMP model, Gs are goroutines, Ms are OS threads and Ps are the permits to run Go code, with GOMAXPROCS of them. Each P has a local run queue, a shared global queue backs it up, idle Ps steal work, the netpoller parks I/O waiters, and sysmon hands off Ps from blocked syscalls and preempts long-running goroutines.