为什么使用std :: thread :: hardware_concurrency()和boost :: thread :: hardware_concurrency()会有区别?

时间:2011-12-16 21:30:17

标签: linux multithreading boost c++11

问题本身的描述非常简单。我正在测试C ++ 11和boost :: thread库中std :: 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

2 个答案:

答案 0 :(得分:20)

查看/ 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

中尚未实现的另一个功能

答案 1 :(得分:6)

您的编译器安装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;
相关问题