cpu.h (863B)
1 2 #if defined(_WIN32) || defined(_WIN64) 3 #include <windows.h> 4 #elif defined(__linux__) 5 #include <unistd.h> 6 #elif defined(__APPLE__) 7 #include <sys/types.h> 8 #include <sys/sysctl.h> 9 #else 10 #error "Unsupported platform" 11 #endif 12 13 static inline int get_cpu_cores() { 14 int num_cores = 0; 15 16 // Windows 17 #if defined(_WIN32) || defined(_WIN64) 18 SYSTEM_INFO sysinfo; 19 GetSystemInfo(&sysinfo); 20 num_cores = sysinfo.dwNumberOfProcessors; // This returns logical processors 21 // Further use GetLogicalProcessorInformation for physical cores... 22 // Linux 23 #elif defined(__linux__) 24 num_cores = sysconf(_SC_NPROCESSORS_ONLN); // This returns logical processors 25 // macOS 26 #elif defined(__APPLE__) 27 size_t size = sizeof(num_cores); 28 sysctlbyname("hw.physicalcpu", &num_cores, &size, NULL, 0); 29 #else 30 num_cores = -1; // Unsupported platform 31 #endif 32 33 return num_cores; 34 }