3

Possible Duplicate:
Programmatically find the number of cores on a machine

I have a multi-threaded C++ program that runs on Windows, MacOS and Linux. Is there an easy way to find out how many cores the machine on which I am running currently has? My system runs best when it has 1 thread for each core.

Community
  • 1
  • 1
vy32
  • 28,461
  • 37
  • 122
  • 246

3 Answers3

3

Use boost::thread::hardware_concurrency() or std::thread::hardware_concurrency() if you have a C++0x compiler.

Yakov Galka
  • 70,775
  • 16
  • 139
  • 220
2

I believe this question may contain your answer:

Programmatically find the number of cores on a machine

Edit: I just voted up ybungalobill's answer. I love boost!

Community
  • 1
  • 1
ClosureCowboy
  • 20,825
  • 13
  • 57
  • 71
0

On a linux system, cat /proc/cpuinfo and parse the output is one way. Dunno if it's the most efficient or not.

Leonard
  • 13,269
  • 9
  • 45
  • 72