zl程序教程

您现在的位置是:首页 >  后端

当前栏目

c++检测cpu内核的数目

C++内核CPU 检测 数目
2023-09-14 09:01:35 时间

以下代码摘自OpenCV源码中的parallel.cpp文件, 翻看源码的时候看到,里面的注释说的事很重要,记录一下。

static
int getNumberOfCPUs_()
{
    /*
     * Logic here is to try different methods of getting CPU counts and return
     * the minimum most value as it has high probablity of being right and safe.
     * Return 1 if we get 0 or not found on all methods.
    */
#if defined CV_CXX11 \
    && !defined(__MINGW32__) /* not implemented (2020-03) */ \

    /*
     * Check for this standard C++11 way, we do not return directly because
     * running in a docker or K8s environment will mean this is the host
     * machines config not the containers or pods and as per docs this value
     * must be "considered only a hint".
    */
    unsigned ncpus = std::thread::hardware_concurrency(); /* If the value is not well defined or not computable, returns 0 */
#else
    unsigned ncpus = 0; /* 0 means we have to find out some other way */
#endif

#if defined _WIN32