POSIX equivalent of boost::thread::hardware_concurrency [duplicate]
Possible Duplicate:
Programmatically find the number of cores on a machine
What is the POSIX or x86, x86-64 specific system call to determine the max number of threads the system can run without over-subscription? Thank you.
It uses C-compatible constructs, so why not just use the actual code? [libs/thread/src/*/thread.cpp]
using pthread library:
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
}
in windows:
unsigned thread::hardware_concurrency()
{
SYSTEM_INFO info={{0}};
GetSystemInfo(&info);
return info.dwNumberOfProcessors;
}
精彩评论