c++ - POSIX 相当于 boost::thread::hardware_concurrency

标签 c++ c linux

<分区>

Possible Duplicate:
Programmatically find the number of cores on a machine

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

最佳答案

它使用与 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;
}

关于c++ - POSIX 相当于 boost::thread::hardware_concurrency,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7341046/

相关文章:

c++ - 如何在 Linux 中找到 'temp' 目录?

c++ - 将时间戳打包成 4 个字节

c++ - qt qgraphicsscene线子类

c++ - 数组中的第 K 个和

c++ - 禁用 QDialogs 默认关闭按钮(左上角 "cross button")?

c - char***在C语言中是什么意思?

c - Unix 中的内存映射 I/O

linux - Hadoop HDFS : DateNode directory on system partition?

linux - Linux 中的 Openwebload

java - 我想编写一个可以在另一个程序中运行和执行操作的程序