POSIX equivalent to boost :: thread :: hardware_concurrency
Possible duplicate:
Programmatically find the number of cores on a machine
What is a POSIX or x86, x86-64 system call to determine the maximum number of threads a system can run without oversubscribing? Thank.
+1
source to share
1 answer
It uses C compatible constructs, so why not just use the actual code? [LIES / thread / src / * / thread.cpp]
using the 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;
}
+5
source to share