Go: GOMAXPROCS & Live Updates
ℹ️ This article is based on Go 1.13.
GOMAXPROCS
controls the maximum number of OS threads that are executing code simultaneously. This can be done while launching your programs or even during the execution. By default, Go sets the value up to the number of logical CPU available, but it has not always been like this.
Default Value
Since Go 1.5, the default value for GOMAXPROCS
has been changed from one to the number of visible CPUs. This change has been possible thanks to the improvements done on the Go scheduler and the context switch on the goroutines. Indeed, in the early days of Go, programs that aimed to work concurrently with frequent goroutines switches suffered from switches between threads.
The proposal for this new value of GOMAXPROCS
provides benchmarks that show this improvement:
- The first benchmark creates a chain of 100 goroutines connected by channels, buffered and unbuffered:
- A second benchmark with the generation of the prime numbers shows how using more core went from a big negative impact to a huge positive impact: