C++ 获得cpu个数

本文转自:http://www.cppblog.com/Chipset/archive/2009/02/20/74403.html

 

//测试CPU核心个数 #if !defined (_WIN32) && !defined (_WIN64) #define LINUX #include <sysconf.h> #else #define WINDOWS #include <windows.h> #endif unsigned core_count() { unsigned count = 1; // 至少一个 #if defined (LINUX) count = sysconf(_SC_NPROCESSORS_CONF); #elif defined (WINDOWS) SYSTEM_INFO si; GetSystemInfo(&si); count = si.dwNumberOfProcessors; #endif return count; } #include <iostream> int main() { unsigned sz = core_count(); std::cout << sz << (1 == sz ? "core" : "cores") << '/n'; } 

你可能感兴趣的:(C++,windows,linux,测试,System)