what is the GOMAXPROCS default value

梦想的初衷 提交于 2019-12-03 06:23:11

问题


Is it guaranteed that GOMAXPROCS is set to 1 when the environment variable of the same name is not set?

This code shows the value:

package main

import (
    "runtime"
    "fmt"
)

func getGOMAXPROCS() int {
    return runtime.GOMAXPROCS(0)
}

func main() {
    fmt.Printf("GOMAXPROCS is %d\n", getGOMAXPROCS())
}

and running it like this:

$ GOMAXPROCS= go run max.go 
GOMAXPROCS is 1

shows that it is 1 in this case, but I am looking for some confirmation here.


回答1:


UPDATE 2018: By default, Go programs run with GOMAXPROCS set to the number of cores available; in prior releases it defaulted to 1.

Starting from Go 1.5, the default value is the number of cores. You only need to explicitly set it if you are not okay with this in newer Go versions.


No, there's no guarantee about what the default is; even though all known implementations use the value '1'. If your code, in absence of the environment variable, requires a specific default value then you should set it in code. Additionally:

GOMAXPROCS 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 local machine can be queried with NumCPU. This call will go away when the scheduler improves.

(Emphasis mine)




回答2:


As Go 1.5 Release Notes says

By default, Go programs run with GOMAXPROCS set to the number of cores available; in prior releases it defaulted to 1.

So starting from Go 1.5, the default value should be the number of cores.




回答3:


Starting with Go 1.5, GOMAXPROCS is set to number of CPUs available by default. However, you can explicitly set it using GOMAXPROCS environment variable or by calling runtime.GOMAXPROCS.

https://docs.google.com/document/d/1At2Ls5_fhJQ59kDK2DFVhFu3g5mATSXqqV5QrxinasI/preview?sle=true



来源:https://stackoverflow.com/questions/17853831/what-is-the-gomaxprocs-default-value

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!