Sobes.tech
Senior

Tell me about the Go scheduler (GMP model)

sobes.tech AI

Answer from AI

The Go scheduler is based on the GMP model — an abstraction that helps efficiently manage concurrency in the language.

  • G (goroutine) — a lightweight thread of execution created by the developer.
  • M (machine) — a system thread of the OS on which goroutines run.
  • P (processor) — a logical processor that connects G and M, managing their execution.

Scheduler operation:

  1. Each P has a queue of Gs ready to run.
  2. M is bound to a P and executes Gs from its queue.
  3. If G blocks (e.g., on I/O), M is released and can serve another P.
  4. The scheduler balances load among Ps, distributing Gs across queues.

This allows Go to efficiently utilize system threads, minimizing overhead and ensuring scalability.

Example of creating a goroutine:

func main() {
    go func() {
        fmt.Println("Hello from goroutine")
    }()
    time.Sleep(time.Second) // Wait for completion
}
Tell me about the Go scheduler (GMP model) — Golang - sobes.tech