C++11 获取 CPU 个数

⌇●﹏●⌇

在 C++11 中 ,要获取计算机上可用的 CPU 核心数量,可以使用 C++ 标准库中的 std::thread::hardware_concurrency() 函数。这个函数会返回一个表示可用核心数量的整数值。需要注意的是,它返回的是一个估计值,可能并不完全准确,因为硬件和操作系统的不同。

举个例子🌰:

#include <iostream>
#include <thread>

int main() {
    // 获取可用的 CPU 核心数量
    unsigned int num_cores = std::thread::hardware_concurrency();

    if (num_cores == 0) {
        std::cout << "Unable to determine the number of CPU cores." << std::endl;
    } else {
        std::cout << "Number of CPU cores: " << num_cores << std::endl;
    }

    return 0;
}

在这个例子中, std::thread::hardware_concurrency() 函数会返回一个表示可用核心数量的整数值,可以用来显示计算机的 CPU 核心数量。

分类: 扫盲 标签: CPP 11

评论

暂无评论数据

暂无评论数据

目录