2011-12-16 244 views
22

问题描述本身非常简单。我正在测试C++ 11和boost :: thread库中std :: thread库的差异。为什么使用std :: thread :: hardware_concurrency()和boost :: thread :: hardware_concurrency()会有区别?

这些输出:

#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 

回答

19

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

6

编译 安装升压的使用是否支持你的目标的方法,而你的 安装升压 编译器不支持此功能,您的目标。

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; 
+0

但实际上boost :: thread可以显示正确的信息4,而C++ 11给我0 ... – derekhh 2011-12-16 21:38:15

相关问题