Golang: how to verify number of processors on which a Go program is running

后端 未结 2 417
后悔当初
后悔当初 2020-12-14 21:12

I am new to Google Go (Golang). My question is related to this post What exactly does runtime.Gosched do?. The structure of code is as copied below. My question, is that whe

相关标签:
2条回答
  • 2020-12-14 21:39

    The number of cores can be inquired by http://golang.org/pkg/runtime/#NumCPU.

    The documentation says: "NumCPU returns the number of logical CPUs on the local machine."

    0 讨论(0)
  • 2020-12-14 21:49

    The largest number of logical CPUs the process can be running on at a given time is no more than the minimum of runtime.GOMAXPROCS(0) and runtime.NumCPU().

    func MaxParallelism() int {
        maxProcs := runtime.GOMAXPROCS(0)
        numCPU := runtime.NumCPU()
        if maxProcs < numCPU {
            return maxProcs
        }
        return numCPU
    }
    
    0 讨论(0)
提交回复
热议问题