This page focuses on concurrency-related runtime functions. For complete runtime documentation, see the official Go runtime package.
Goroutine Management
Gosched
Yields the processor to allow other goroutines to run.Yields the processor, allowing other goroutines to run. It does not suspend the current goroutine, so execution resumes automatically.
Gosched is useful in tight loops when you want to be cooperative with the scheduler, but in most cases, the Go scheduler handles this automatically.
Goexit
Terminates the goroutine that calls it.Terminates the goroutine that calls it. No other goroutine is affected. Goexit runs all deferred calls before the goroutine exits. Calling Goexit from the main goroutine terminates that goroutine without func main returning.
NumGoroutine
Returns the number of goroutines that currently exist.Returns the number of goroutines that currently exist. This includes all goroutines, whether running, blocked, or ready to run.
Concurrency Configuration
GOMAXPROCS
Sets the maximum number of CPUs that can execute simultaneously.Sets the maximum number of CPUs that can be executing simultaneously and returns the previous setting. If n < 1, it does not change the current setting. The number of logical CPUs on the machine can be queried with NumCPU.
By default, GOMAXPROCS is set to the value of NumCPU. Starting in Go 1.5, the runtime automatically sets GOMAXPROCS to match the number of available CPUs.
NumCPU
Returns the number of logical CPUs usable by the current process.Returns the number of logical CPUs usable by the current process. The set of available CPUs is checked by querying the operating system at process startup. Changes to operating system CPU allocation after process startup are not reflected.
Runtime Information
NumCgoCall
Returns the number of cgo calls made by the current process.Returns the number of cgo calls made by the current process.
Goroutine Scheduler
The Go runtime uses a work-stealing scheduler to distribute goroutines across available processors (P). Understanding these concepts can help you write more efficient concurrent programs.Key Concepts
G - Goroutine
G - Goroutine
A goroutine represents a lightweight thread of execution. Goroutines are multiplexed onto OS threads by the Go runtime.
M - Machine (OS Thread)
M - Machine (OS Thread)
An M represents an OS thread. The runtime creates and manages a pool of OS threads to execute goroutines.
P - Processor
P - Processor
A P represents a resource required to execute Go code. The number of Ps is set by GOMAXPROCS. Each M must have an associated P to execute Go code.
Best Practices
Let the runtime manage GOMAXPROCS
Let the runtime manage GOMAXPROCS
In most cases, you don’t need to set GOMAXPROCS manually. The runtime automatically sets it to match the number of available CPUs, which is optimal for most workloads.
Avoid calling Gosched in normal code
Avoid calling Gosched in normal code
The Go scheduler is cooperative and preemptive. You rarely need to call Gosched explicitly. Let the runtime handle scheduling automatically.
Use Goexit carefully
Use Goexit carefully
Goexit should only be used in specific scenarios where you need to terminate a goroutine early while still running deferred functions. Consider using context cancellation instead.
Monitor goroutine count in production
Monitor goroutine count in production
Use NumGoroutine() to monitor for goroutine leaks. A steadily increasing goroutine count often indicates a leak.
Common Patterns
Setting Concurrency Limits
Monitoring Goroutine Health
CPU-Intensive Task Configuration
Graceful Goroutine Termination
Performance Considerations
Goroutine Creation Cost
Goroutines are cheap (about 2KB initial stack), but not free. Creating millions of goroutines can consume significant memory. Use worker pools for bounded concurrency.
Context Switching
The Go scheduler performs context switches between goroutines. Too many active goroutines can increase context switching overhead. Monitor with NumGoroutine().
P Allocation
Each P maintains a local run queue of goroutines. Setting GOMAXPROCS too high (beyond available CPUs) doesn’t improve performance and can increase overhead.
Related Packages
- sync: Provides synchronization primitives like Mutex, WaitGroup, and Once
- context: Manages goroutine lifecycles with cancellation and deadlines
- runtime/debug: Provides debugging and introspection capabilities
- runtime/trace: Provides execution tracing for program analysis