views:

85

answers:

4

Is there a way to get the nr of processing units like cpus or cores in a system? I am going to write an app with pthreads so if there is a posix call that would be great. I know about reading from /proc/cpuinfo but that is not so portable. Is there another more portable way?

+1  A: 

There is no POSIX way that I know of. You need to fall back and do per-platform checks, or find a library that already does does checks for you.

This page has downloadable code for a number of UNIX implementations.

unwind
A: 

Not to my knowledge. Under windows, for example, you'd get that from GetSystemInfo. You can probably get this info on x86 CPUs using a CPUID assembler call, but thats no use on non-x86 platforms.

Goz
+5  A: 

The POSIX (threads) committee considered supporting such an operation, but then refused to. People have collected information on how to do that on various systems.

Martin v. Löwis
The last two paragraphs of that newsgroup posting by Butenhof are particularly apt I would say; although I would add that the only person who can really answer the question "would my app benefit from creating another thread" is the system administrator, which implies that "number of threads" should be a user-tweakable.
caf
A: 

I found a similar (duplicate) question but for C++ http://stackoverflow.com/questions/150355/programmatically-find-the-number-of-cores-on-a-machine

Per Arneng