팁
[팁]Programmatically find the number of cores on a machine
Binceline
2013. 10. 6. 06:10
C++11
http://en.cppreference.com/w/cpp/thread/thread/hardware_concurrency
//may return 0 when not able to detect
unsigned concurentThreadsSupported = std::thread::hardware_concurrency();
Win32:
SYSTEM_INFO sysinfo;
GetSystemInfo( &sysinfo );
numCPU = sysinfo.dwNumberOfProcessors;
Linux, Solaris, & AIX and Mac OS X (for all OS releases >= 10.4, i.e., Tiger onwards) - per comments:
numCPU = sysconf( _SC_NPROCESSORS_ONLN );
FreeBSD, MacOS X, NetBSD, OpenBSD, etc.:
int mib[4];
size_t len = sizeof(numCPU);
/* set the mib for hw.ncpu */
mib[0] = CTL_HW;
mib[1] = HW_AVAILCPU; // alternatively, try HW_NCPU;
/* get the number of CPUs from the system */
sysctl(mib, 2, &numCPU, &len, NULL, 0);
if( numCPU < 1 )
{
mib[1] = HW_NCPU;
sysctl( mib, 2, &numCPU, &len, NULL, 0 );
if( numCPU < 1 )
{
numCPU = 1;
}
}
HPUX:
numCPU = mpctl(MPC_GETNUMSPUS, NULL, NULL);
IRIX:
numCPU = sysconf( _SC_NPROC_ONLN );
Mac OS X (10.5 and newer) or iOS (any version) using Objective-C:
NSUInteger a = [[NSProcessInfo processInfo] processorCount];
NSUInteger b = [[NSProcessInfo processInfo] activeProcessorCount];
반응형