Programmatically find the number of cores on a machine

前端 未结 19 2451
刺人心
刺人心 2020-11-22 16:38

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/*

19条回答
  •  我在风中等你
    2020-11-22 17:22

    This functionality is part of the C++11 standard.

    #include 
    
    unsigned int nthreads = std::thread::hardware_concurrency();
    

    For older compilers, you can use the Boost.Thread library.

    #include 
    
    unsigned int nthreads = boost::thread::hardware_concurrency();
    

    In either case, hardware_concurrency() returns the number of threads that the hardware is capable of executing concurrently based on the number of CPU cores and hyper-threading units.

提交回复
热议问题