开发者

POSIX equivalent of boost::thread::hardware_concurrency [duplicate]

开发者 https://www.devze.com 2023-04-03 03:57 出处:网络
This question already has answers here: Closed 11 years ago. 开发者_如何学JAVA Possible Duplicate:
This question already has answers here: Closed 11 years ago. 开发者_如何学JAVA

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;
}
0

精彩评论

暂无评论...
验证码 换一张
取 消