英文:
What is the GOMAXPROCS default value?
问题
当环境变量与GOMAXPROCS的名称相同时,是否保证GOMAXPROCS设置为1?
这段代码显示了该值:
package main
import (
"runtime"
"fmt"
)
func getGOMAXPROCS() int {
return runtime.GOMAXPROCS(0)
}
func main() {
fmt.Printf("GOMAXPROCS为%d\n", getGOMAXPROCS())
}
像这样运行它:
$ GOMAXPROCS= go run max.go
GOMAXPROCS为1
显示在这种情况下它是1,但我在这里寻求一些确认。
英文:
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
得分: 72
更新 2018:
默认情况下,Go程序运行时GOMAXPROCS设置为可用的核心数;在之前的版本中,默认设置为1。
从Go 1.5开始,默认值是核心数。
只有在新版本的Go中不满意此默认值时,才需要显式设置它。
不,没有关于默认值的保证;尽管所有已知的实现都使用值'1'。如果你的代码在没有环境变量的情况下需要特定的默认值,那么你应该在代码中设置它。此外:
> GOMAXPROCS设置可以同时执行的最大CPU数,并返回先前的设置。如果n < 1,则不会更改当前设置。可以使用NumCPU查询本地机器上的逻辑CPU数。当调度器改进时,此调用将消失。
(重点是我的)
英文:
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
得分: 44
根据Go 1.5发布说明所说
> 默认情况下,Go程序运行时GOMAXPROCS设置为可用的核心数;在之前的版本中,默认值为1。
所以从Go 1.5开始,默认值应该是核心数。
英文:
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
得分: 14
从Go 1.5开始,默认情况下,GOMAXPROCS被设置为可用的CPU数量。但是,您可以通过设置GOMAXPROCS环境变量或调用runtime.GOMAXPROCS来显式设置它。
https://docs.google.com/document/d/1At2Ls5_fhJQ59kDK2DFVhFu3g5mATSXqqV5QrxinasI/preview?sle=true
英文:
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
答案4
得分: -1
package main
import (
"fmt"
"runtime"
)
func main() {
fmt.Println(runtime.GOMAXPROCS(runtime.NumCPU() - 1))
}
这将输出您当前线程的数量,
始终添加此代码以控制您的线程,
不要依赖于其他人的默认设置。
package main
import (
"fmt"
"runtime"
)
func main() {
runtime.GOMAXPROCS(runtime.NumCPU() - 1)
// and your code goes brrrrrrrrrrrrr....
}
对于那些会问为什么是-1的人
GOMAXPROCS设置可以同时执行的最大CPU数量,并返回先前的设置。它默认为runtime.NumCPU的值。**如果n < 1,则不会更改当前设置。**当调度程序改进时,此调用将消失。
原文链接。
英文:
package main
import (
"fmt"
"runtime"
)
func main() {
fmt.Println(runtime.GOMAXPROCS(runtime.NumCPU() - 1))
}
this will output your current thread's,
always add this to have a control of ur threading
and don't rely on someones defauls.
package main
import (
"fmt"
"runtime"
)
func main() {
runtime.GOMAXPROCS(runtime.NumCPU() - 1)
// and your code goes brrrrrrrrrrrrr....
}
and for those who will ask why -1
> GOMAXPROCS sets the maximum number of CPUs that can be executing simultaneously and returns the previous setting. It defaults to the value of runtime.NumCPU. If n < 1, it does not change the current setting. This call will go away when the scheduler improves.
utility is our all.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论