windows下和linux下获取cpu核数
windows下:
#include "windows.h" SYSTEM_INFO sysInfo; GetSystemInfo( &sysInfo ); printf("now system cpu num is %d\n", sysInfo.dwNumberOfProcessors);
跨平台:
#ifndef __linux__ #include "windows.h" #else #include "unistd.h" #include "sys/sysinfo.h" #endif #ifndef __linux__ SYSTEM_INFO sysInfo; GetSystemInfo( &sysInfo ); printf("system cpu num is %d\n", sysInfo.dwNumberOfProcessors); #else printf("system cpu num is %d \n", sysconf(_SC_NPROCS_CONF)); printf("system enable num is %d\n", sysconf(_SC_NPROCS_ONLN)); //GNU way printf("system cpu num is %d\n", get_nprocs_conf()); printf("system enable num is %d\n", get_nprocs()); #endif
转载于:https://blog.csdn.net/hanxv_1987/article/details/79521102