Go ランタイムには、多数のゴルーチンを少数の OS スレッドに多重化するスケジューラが含まれています。このM:N スケジューリング(M 個のゴルーチンを N 個の OS スレッド上で実行)により、ゴルーチンが非常に軽量で、Go の並行処理が拡張性に優れています。これを理解することで、ゴルーチンのパフォーマンスを説明できます。
G-M-P モデル
G (Goroutine) — your concurrent task (lightweight, ~2KB stack to start)
M (Machine) — an OS thread (the actual thread the OS schedules)
P (Processor) — a logical processor / scheduling context; holds a queue of runnable Gs
(the number of P's = GOMAXPROCS, default = number of CPU cores)
The scheduler runs G's on M's, coordinated through P's:
Each P has a local run queue of goroutines; an M must hold a P to run G's.
