duanlu6114 2012-11-05 15:03
浏览 139
已采纳

Golang:如何验证运行Go程序的处理器数量

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 < 100000000; ji++ {
        for io := 1; io < 10; io++ {
            //Some computations
        }
    }
    runtime.Gosched()

    wg.Done()
}

func main() {
    wg.Add(1)
    runtime.GOMAXPROCS(1) // or 2 or 4
    go doTasks()
    doTasks()
    wg.Wait()
}
  • 写回答

2条回答 默认 最新

  • doujiao3074 2012-11-06 05:40
    关注

    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
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 linux驱动,linux应用,多线程
  • ¥20 我要一个分身加定位两个功能的安卓app
  • ¥15 基于FOC驱动器,如何实现卡丁车下坡无阻力的遛坡的效果
  • ¥15 IAR程序莫名变量多重定义
  • ¥15 (标签-UDP|关键词-client)
  • ¥15 关于库卡officelite无法与虚拟机通讯的问题
  • ¥15 目标检测项目无法读取视频
  • ¥15 GEO datasets中基因芯片数据仅仅提供了normalized signal如何进行差异分析
  • ¥100 求采集电商背景音乐的方法
  • ¥15 数学建模竞赛求指导帮助