什么是 GOMAXPROCS 默认值? [英] What is the GOMAXPROCS default value?

查看:54
本文介绍了什么是 GOMAXPROCS 默认值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在没有设置同名环境变量的情况下,能保证GOMAXPROCS设置为1吗?

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

此代码显示值:

package main

import (
    "runtime"
    "fmt"
)

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

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

并像这样运行它:

$ GOMAXPROCS= go run max.go 
GOMAXPROCS is 1

在这种情况下显示为 1,但我正在寻找一些确认.

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

推荐答案

2018 年更新:默认情况下,Go 程序运行时 GOMAXPROCS 设置为可用内核数;在之前的版本中,它默认为 1.

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

从 Go 1.5 开始,默认值为核心数.如果您在较新的 Go 版本中对此不满意,则只需明确设置它.

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.

不,不能保证默认值是什么;即使所有已知的实现都使用值1".如果您的代码在没有环境变量的情况下需要特定的默认值,那么您应该在代码中设置它.另外:

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 设置可以同时执行的最大 CPU 数量并返回之前的设置.如果 n <1、它不改变当前设置.可以用NumCPU查询本地机器上逻辑CPU的数量.当调度程序改进时,此调用将消失.

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.

(强调我的)

这篇关于什么是 GOMAXPROCS 默认值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆