2011-09-07 26 views
1

可能重複:
Programmatically find the number of cores on a machinePOSIX等價的boost ::螺紋:: hardware_concurrency

什麼是POSIX或x86,x86-64的特定系統調用來確定的最大數量系統可以在沒有超額訂閱的情況下運行線程?謝謝。

+1

['螺紋:: hardware_concurrency()'](HTTP: //www.boost.org/doc/libs/1_37_0/doc/html/thread/thread_management.html#thread.thread_management.thread.hardware_concurrency)基本上告訴您運行的處理器核心的數量。 [請參閱此堆棧溢出答案以計算各種平臺上可用的CPU數量](http://stackoverflow.com/questions/150355/programmatically-find-the-number-of-cores-on-a-machine/150971#150971)。 –

+0

除非您知道您是唯一正在運行的進程,並且您的所有線程都將保持滿負荷,否則「可以在沒有超額訂閱的情況下運行的線程數」不是一個非常明確的概念,並且可以動態變化。 –

回答

5

它使用C兼容的構造,所以爲什麼不使用實際的代碼?

unsigned thread::hardware_concurrency() 
{ 
#if defined(PTW32_VERSION) || defined(__hpux) 
    return pthread_num_processors_np(); 
#elif defined(__APPLE__) || defined(__FreeBSD__) 
    int count; 
    size_t size=sizeof(count); 
    return sysctlbyname("hw.ncpu",&count,&size,NULL,0)?0:count; 
#elif defined(BOOST_HAS_UNISTD_H) && defined(_SC_NPROCESSORS_ONLN) 
    int const count=sysconf(_SC_NPROCESSORS_ONLN); 
    return (count>0)?count:0; 
#elif defined(_GNU_SOURCE) 
    return get_nprocs(); 
#else 
    return 0; 
#endif 
} 

在窗口::使用pthread庫[庫/線程/ SRC/*/thread.cpp]

unsigned thread::hardware_concurrency() 
{ 
    SYSTEM_INFO info={{0}}; 
    GetSystemInfo(&info); 
    return info.dwNumberOfProcessors; 
} 
+0

對於頭文件:http://stackoverflow.com/questions/27981038/error-get-nprocs-was-not-declared-in-this-scope – gsamaras