C代码
#include <sys/sysctl.h>
unsigned cpuCount() {
int mib[2] = { CTL_HW, HW_AVAILCPU };
unsigned cpuCount;
size_t sizeOfCpuCount = sizeof(cpuCount);
int status = sysctl(mib, 2, &cpuCount, &sizeOfCpuCount, NULL, 0);
if (status == 0) {
return cpuCount;
} else {
return -1;
}
}