24

The description of the problem itself is pretty simple. I'm testing the differences of std::thread library in C++11 and boost::thread library.

The output of these:

#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;
}

gives me different results:

0
4

Why is that?

PS: The version of the gcc package is 4.6.2-1.fc16 (x86_64). I'm using

g++ test.cc -Wall -std=c++0x -lboost_thread-mt -lpthread
Brian Cain
  • 14,403
  • 3
  • 50
  • 88
derekhh
  • 5,272
  • 11
  • 40
  • 60

2 Answers2

22

After reviewing /usr/include/c++/4.6.2/thread

it can be seen that the implementation is actually:

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

So problem solved. It's just another feature that hasn't been implemented in gcc 4.6.2

derekhh
  • 5,272
  • 11
  • 40
  • 60
6

The method employed by your compiler installation of boost is supported for your target, whereas your installation of boost compiler does not support this feature for your target.

TFM says:

The number of hardware threads available on the current system (e.g. number of CPUs or cores or hyperthreading units), or 0 if this information is not available.

EDIT: scratch that, reverse it.

EDIT2: This feature is present on the trunk, but absent in 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;
Brian Cain
  • 14,403
  • 3
  • 50
  • 88
  • But actually boost::thread can display the correct information 4, whereas c++11 gives me 0... – derekhh Dec 16 '11 at 21:38
  • @derekhh: its very likely that your c++11 implementation is just a skeleton and doesn't actually work – Daniel Dec 16 '11 at 21:41
  • g++ 4.6.2 targeting `linux-x86_64`, or ...? – Brian Cain Dec 16 '11 at 22:10
  • @BrianCain: yeap, it is x86_64. – derekhh Dec 16 '11 at 22:13
  • More info about its pedigree: http://gcc.gnu.org/viewcvs/trunk/libstdc%2B%2B-v3/src/thread.cc?view=log – Brian Cain Dec 16 '11 at 22:29
  • 6
    @derekhh Indeed, C++11 support in gcc 4.6.x is obviously a "work in progress _(at the time the compiler was released)_"; the standard explicitly makes `0` an acceptable fallback value when the real value isn't known, and gcc 4.6.x took advantage of that. C++11 support in gcc 4.7 is much better, and it does provide an accurate value for `std::thread::hardware_concurrency()`. – snogglethorpe Dec 20 '11 at 02:13