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

后端 未结 2 425
后悔当初
后悔当初 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: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
    }
    

提交回复
热议问题