As we know that There is a runtime when Golang running. The runtime perform the scheduled tasks(goroutine) in user space rather than kernel, so it's more lightweight. It do a better tradeoff between system resource usage and performance, especially in IO tasks. In this article, I'll show you Golang scheduler's history, Goroutine scheduler GMP's design pattern, and some cases how does GMP handle.
We know why we need a scheduler in previous article. And GM pattern also have a bad performance, to fix this problem, a GMP pattern introduced into golang.
In this pattern, threads are the physical workers, scheduler should dispatch goroutines to one thread.
GMP Pattern
GOMAXPROCS
value is determinedGoroutine scheduler and OS scheduler are connected using M, every M is a physical OS thread, OS scheduler dispatch M running in a real CPU core.
Determined by GOMAXPROCS
environment variable or GOMAXPROCS()
function in runtime package. That means there are GOMAXPROCS goroutines run concurrent at any time.
SetMaxThreads()
function in runtime/debug package can setup the max threads count.