久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

以編程方式查找機器上的內核數

Programmatically find the number of cores on a machine(以編程方式查找機器上的內核數)
本文介紹了以編程方式查找機器上的內核數的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

有沒有辦法以獨立于平臺的方式從 C/C++ 確定一臺機器有多少個內核?如果不存在這樣的東西,那么按平臺(Windows/*nix/Mac)確定它怎么樣?

Is there a way to determine how many cores a machine has from C/C++ in a platform-independent way? If no such thing exists, what about determining it per-platform (Windows/*nix/Mac)?

推薦答案

C++11

#include <thread>

//may return 0 when not able to detect
const auto processor_count = std::thread::hardware_concurrency();

參考:std::thread::hardware_concurrency

在 C++11 之前的 C++ 中,沒有可移植的方式.相反,您需要使用以下一種或多種方法(由適當的 #ifdef 行保護):

In C++ prior to C++11, there's no portable way. Instead, you'll need to use one or more of the following methods (guarded by appropriate #ifdef lines):

  • Win32

SYSTEM_INFO sysinfo;
GetSystemInfo(&sysinfo);
int numCPU = sysinfo.dwNumberOfProcessors;

  • Linux、Solaris、AIX 和 Mac OS X >=10.4(即 Tiger 以上)

    int numCPU = sysconf(_SC_NPROCESSORS_ONLN);
    

  • FreeBSD、MacOS X、NetBSD、OpenBSD 等

    int mib[4];
    int numCPU;
    std::size_t len = sizeof(numCPU); 
    
    /* set the mib for hw.ncpu */
    mib[0] = CTL_HW;
    mib[1] = HW_AVAILCPU;  // alternatively, try HW_NCPU;
    
    /* get the number of CPUs from the system */
    sysctl(mib, 2, &numCPU, &len, NULL, 0);
    
    if (numCPU < 1) 
    {
        mib[1] = HW_NCPU;
        sysctl(mib, 2, &numCPU, &len, NULL, 0);
        if (numCPU < 1)
            numCPU = 1;
    }
    

  • HPUX

    int numCPU = mpctl(MPC_GETNUMSPUS, NULL, NULL);
    

  • IRIX

    int numCPU = sysconf(_SC_NPROC_ONLN);
    

  • Objective-C(Mac OS X >=10.5 或 iOS)

    NSUInteger a = [[NSProcessInfo processInfo] processorCount];
    NSUInteger b = [[NSProcessInfo processInfo] activeProcessorCount];
    

  • 這篇關于以編程方式查找機器上的內核數的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

    【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!

    相關文檔推薦

    Algorithm to convert RGB to HSV and HSV to RGB in range 0-255 for both(將 RGB 轉換為 HSV 并將 HSV 轉換為 RGB 的算法,范圍為 0-255)
    How to convert an enum type variable to a string?(如何將枚舉類型變量轉換為字符串?)
    When to use inline function and when not to use it?(什么時候使用內聯函數,什么時候不使用?)
    Examples of good gotos in C or C++(C 或 C++ 中好的 goto 示例)
    Significance of ios_base::sync_with_stdio(false); cin.tie(NULL);(ios_base::sync_with_stdio(false) 的意義;cin.tie(NULL);)
    Is TCHAR still relevant?(TCHAR 仍然相關嗎?)
    主站蜘蛛池模板: 国产午夜精品视频 | av电影手机在线看 | 久草免费视 | 久久精品国产一区二区电影 | 国产成人精品一区二区三 | 国内精品久久久久久 | 日韩在线播放第一页 | 国产91在线视频 | 久久久91精品国产一区二区三区 | 91精品久久久久久久久 | 精品二区视频 | 国产精品久久久久久久久久久久久久 | av天天干 | 欧美性久久 | 三级在线视频 | 国产精品国色综合久久 | 亚洲欧美一区二区三区在线 | 911精品美国片911久久久 | 国产欧美日韩综合精品一区二区 | 久久99精品久久久久久国产越南 | 亚洲午夜精品视频 | 盗摄精品av一区二区三区 | 激情婷婷成人 | 亚洲精品亚洲人成人网 | 免费黄色片在线观看 | 中文字幕一区二区三区四区五区 | 久久高清| 国产午夜精品视频 | 色综合区| 一级看片 | 亚洲精品久久久一区二区三区 | 日韩三级在线 | 影音先锋中文字幕在线观看 | 久草a√ | 激情毛片| 精品国产18久久久久久二百 | 欧美日韩综合视频 | 亚洲一页 | 一区二区三区视频在线免费观看 | 免费污视频 | 亚洲一区二区中文字幕 |