package cpu import ( "math" "runtime" ) var maxThreads int // init defaults to 90% of available cores (at least 1). func init() { SetMaxThreads(-1) } // SetMaxThreads updates the maximum CPU worker count used by CPU kernels. // Passing n <= 0 picks 90% of available cores. Clamped to [1, NumCPU]. // It also sets GOMAXPROCS to match to avoid oversubscription. func SetMaxThreads(n int) { cores := runtime.NumCPU() if cores < 1 { cores = 1 } if n <= 0 || n > cores { n = int(math.Ceil(0.9 * float64(cores))) if n < 1 { n = 1 } } maxThreads = n runtime.GOMAXPROCS(n) } // MaxThreads returns the configured worker count. func MaxThreads() int { return maxThreads }