2011-12-16 242 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

相關問題