Runtime and memory · 3 MIN READ
Goroutines, OS threads and GOMAXPROCS
Explain concurrency versus parallel execution without assuming a goroutine is an OS thread.
A goroutine is a Go execution task scheduled by the runtime. An OS thread is an operating-system execution resource. The runtime multiplexes tasks onto threads; counting goroutines therefore does not tell you how many cores are doing useful work.
Use the G, M, P model carefully
In runtime terminology G represents a goroutine, M an OS thread, and P the scheduler resources needed to execute ordinary Go code. GOMAXPROCS controls how many Ps execute Go code at once. It is not a limit on total goroutines or a universal limit on all process threads.
CPU work and waiting behave differently
Several CPU-bound tasks need available execution capacity to run in parallel. Waiting network operations can overlap while other work proceeds. Starting ten thousand CPU-heavy goroutines does not create ten thousand cores and may add scheduling overhead. Native defaults can account for runtime and container configuration; inspect the actual service instead of copying a magic setting.
fmt.Println("Go execution slots:", runtime.GOMAXPROCS(0))
fmt.Println("goroutines now:", runtime.NumGoroutine())Passing zero queries the current GOMAXPROCS value. NumGoroutine is a snapshot and includes runtime/application work; it is a diagnostic signal, not a precise request counter.
Choose the right concurrency level
For CPU work, benchmark a bounded pool around the available CPU budget. For I/O work, use downstream concurrency, queue and deadline limits. A service can be overloaded with many waiting tasks even when CPU looks low. The iOS app's WebAssembly execution environment does not demonstrate native OS-thread behavior; these examples describe native Go services.
TAKE THIS WITH YOU
Goroutines express concurrency. The runtime and OS determine parallel execution within available CPU and downstream budgets.
Check your understanding
Which statement is correct about gomaxprocs?
Go to the source
Progress stays in this browser. No account needed.