Context switching suspends one execution context and resumes another. A CPU core switches between OS threads by saving execution state, while the Go runtime switches goroutines on and off OS threads. Goroutines use less memory than OS threads, making their switches faster. section 8.2.1
Avoid treating more goroutines as automatic parallelism:
- For CPU-bound work, too many goroutines can make threads share execution time, increasing context switches. section 8.5
- A worker pool near the number of available threads can reduce unnecessary switching when the workload is CPU-bound. section 8.5
- I/O-bound pools depend mainly on the external system, so workload classification comes before choosing the pool size. section 8.5
Sizing a CPU-bound worker pool
Use runtime.GOMAXPROCS(0) to inspect the configured processor limit, then size a CPU-bound worker pool accordingly. A fixed pool can receive tasks through a shared channel, process them concurrently, and wait for completion; validate the choice with benchmarks rather than assuming it is optimal. section 8.5








