【问题标题】:Why is there a difference using std::thread::hardware_concurrency() and boost::thread::hardware_concurrency()?为什么使用 std::thread::hardware_concurrency() 和 boost::thread::hardware_concurrency() 有区别?
【发布时间】:2012-01-22 08:19:42
【问题描述】:

问题本身的描述非常简单。我正在测试 C++11 中 std::thread 库和 boost::thread 库的区别。

这些的输出:

#include <iostream>
#include <thread>
#include <boost/thread.hpp>

int main() {
  std::cout << std::thread::hardware_concurrency() << std::endl;
  std::cout << boost::thread::hardware_concurrency() << std::endl;
  return 0;
}

给了我不同的结果:

0
4

这是为什么呢?

PS:gcc包的版本是4.6.2-1.fc16(x86_64)。我正在使用

g++ test.cc -Wall -std=c++0x -lboost_thread-mt -lpthread

【问题讨论】:

    标签: linux multithreading boost c++11


    【解决方案1】:

    查看/usr/include/c++/4.6.2/thread

    可以看出,实现其实是:

    // Returns a value that hints at the number of hardware thread contexts.
    static unsigned int
    hardware_concurrency()
    { return 0; }
    

    所以问题解决了。这只是 gcc 4.6.2 中尚未实现的另一个功能

    【讨论】:

      【解决方案2】:

      您的目标支持 compiler 安装 boost 所采用的方法,而您的 boost 安装 编译器不支持您的目标使用此功能。

      TFM 说:

      当前系统上可用的硬件线程数(例如 CPU 或内核或超线程单元的数量),如果此信息不可用,则为 0。

      编辑:从头开始,反转它。

      EDIT2:此功能在 the trunk 上存在,但在 4.6.2 中不存在:

      ~/tmp/gcc-4.6.2/libstdc++-v3/src> wc -l thread.cc
      104 thread.cc
      ~/tmp/gcc-4.6.2/libstdc++-v3/src> grep concurrency thread.cc | wc -l
      0
      ~/tmp/gcc-4.6.2/libstdc++-v3> grep -C 2 VERIFY testsuite/30_threads/thread/members/hardware_concurrency.cc
      
        // Current implementation punts on this.
        VERIFY( std::thread::hardware_concurrency() == 0 );
      
        return 0;
      

      【讨论】:

      • 但实际上 boost::thread 可以显示正确的信息 4,而 c++11 给我的是 0...
      • @derekhh:很可能您的 c++11 实现只是一个框架,实际上并不能正常工作
      • g++ 4.6.2 以linux-x86_64 为目标,还是...?
      • @BrianCain:是的,它是 x86_64。
      • @derekhh 事实上,gcc 4.6.x 中的 C++11 支持显然是“正在进行的工作(在编译器发布时)”;当实际值未知时,该标准明确地使0 成为可接受的后备值,而 gcc 4.6.x 就利用了这一点。 gcc 4.7 中的 C++11 支持要好得多,它确实为 std::thread::hardware_concurrency() 提供了准确的值。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-10-05
      • 2011-11-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-09-02
      • 2011-12-15
      相关资源
      最近更新 更多