A hybrid threading system combines lightweight user-level execution units with multiple kernel-level threads. In Go, goroutines run through runtime-managed queues attached to kernel threads, allowing work to use multiple processors instead of remaining on one operating-system thread. section 2.3.2
The model works through several cooperating layers:
- M:N mapping connects M goroutines to N kernel-level threads, rather than mapping every goroutine directly to an operating-system thread. section 2.3.2
- Each kernel thread manages a queue of goroutines, so multiple kernel threads can execute goroutines on separate processor cores. section 2.3.2
- When a goroutine blocks on I/O or work becomes unbalanced, the runtime can create or reuse another thread and move work through work stealing. section 2.6
Inspecting available parallelism
A small Go program can print the number of logical processors and the current GOMAXPROCS setting. The runtime normally initializes GOMAXPROCS from NumCPU(), while calling GOMAXPROCS with a value below one reports the current setting without changing it. For example, execution may print “Number of CPUs: 8” and “GOMAXPROCS: 8.”
