프로그램으로 컴퓨터 코어수를 알 수 있을까요?
조회수 4280회
1 답변
-
C++11이상이라면 std::thread::hardware_concurrency를 보세요
//detect못 할 때는 0을 리턴합니다. unsigned concurentThreadsSupported = std::thread::hardware_concurrency();
Win32:
SYSTEM_INFO sysinfo; GetSystemInfo( &sysinfo ); numCPU = sysinfo.dwNumberOfProcessors;
Linux, Solaris, & AIX and Mac OS X (10.4 이상):
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; // 이게 안 된다면, 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];
댓글 입력