프로그램으로 컴퓨터 코어수를 알 수 있을까요?

조회수 4257회

플랫폼이랑 관계 없이 C/C++써서 몇코어인지를 알아내는 방법 있을까요

그런 방법 없으면 플랫폼따라서 써주세요

1 답변

  • 좋아요

    0

    싫어요
    채택 취소하기

    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];
    

답변을 하려면 로그인이 필요합니다.

프로그래머스 커뮤니티는 개발자들을 위한 Q&A 서비스입니다. 로그인해야 답변을 작성하실 수 있습니다.

(ಠ_ಠ)
(ಠ‿ಠ)