【问题标题】:POSIX equivalent of boost::thread::hardware_concurrency [duplicate]POSIX 等效于 boost::thread::hardware_concurrency [重复]
【发布时间】:2011-11-12 13:15:42
【问题描述】:

可能重复:
Programmatically find the number of cores on a machine

什么是 POSIX 或 x86、x86-64 特定系统调用来确定系统在不超额订阅的情况下可以运行的最大线程数?谢谢。

【问题讨论】:

标签: c++ c linux


【解决方案1】:

它使用与 C 兼容的结构,那么为什么不直接使用实际代码呢? [libs/thread/src/*/thread.cpp]

使用 pthread 库:

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
}

在窗口中:

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

【讨论】:

猜你喜欢
  • 2012-01-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-10-05
  • 2017-02-26
  • 2012-08-23
相关资源
最近更新 更多