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

匿名 (未验证) 提交于 2019-12-03 01:48:02

问题:

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 when I change the number of processor in GOMAXPROCS, how do I verify how many processors it is running on. When I do 'top', it shows a.out process which consumes 100% or less resources even when GOMAXPROCS is more than 1. I would be grateful for your help.

package main  import (     "fmt"     "runtime"     "sync" )  var wg sync.WaitGroup  func doTasks() {     fmt.Println(" Doing task ")     for ji := 1; ji 

回答1:

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 


回答2:

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."



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