➤ 解構 Go 運行時:從 GMP 模型理解任務調度機制
✤ https://internals-for-interns.com/posts/go-runtime-scheduler/
本文深入探討 Go 語言運行時的核心——「調度器」。作者詳細解析了著名的 GMP 模型,解釋 Go 如何透過將輕量級的 Goroutine(G)映射到作業系統執行緒(M),並利用處理器上下文(P)作為中介,實現高效的並發處理。這種設計巧妙地解決了系統呼叫導致的阻塞問題,並透過全局與本地緩存機制,最小化了鎖的競爭,確保了程式在有限 CPU 資源下能流暢運行數百萬個 Goroutine。
+ 這是我讀過對 GMP 模型解釋最清晰的文章之一。以前總是不太理解為什麼需要 P,讀完才明白這是為了讓調度資源與系統執行緒解耦。
+ 對於剛開始接觸 Go 併發編程的人來說,這簡直是必讀指南。特別是關於 g0 棧的管理以及 P 的角色說明,非常有技術深度。
#Go #Runtime #Scheduler #Concurrency

The Scheduler | Internals for Interns
In the previous article we explored how Go’s memory allocator manages heap memory — grabbing large arenas from the OS, dividing them into spans and size classes, and using a three-level hierarchy (mcache, mcentral, mheap) to make most allocations lock-free. A key detail was that each P (processor) gets its own memory cache. But we never really explained what a P is, or how the runtime decides which goroutine runs on which thread. That’s the scheduler’s job, and that’s what we’re exploring today.



