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

问题本身的描述非常简单。我正在测试 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

解答

审查后 /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 中尚未实现的另一个功能

Logo

更多推荐