views:

267

answers:

4

I'm writing a program in C on windows that needs to run as many threads as available cores. But I dont know how to get the number of cores. Any ideas?

+3  A: 

You can call the GetSystemInfo WinAPI function; it returns a SYSTEM_INFO struct, which has the number of processors (which is the number of cores on a system with multiple core CPUs).

James McNellis
What about systems with multiple multi-core CPUs? I'm pretty sure Windows drills down to that detail somewhere, although I don't know if it matters here.
Matt Olenik
+1  A: 

You can read NUMBER_OF_PROCESSORS environment variable.

Josip Medved
It's generally better to call an API function rather than rely on a secondary environment variable (which could be changed by something else before running your program).
Greg Hewgill
A: 

Even though the question deals with .NET and yours with C, the basic responses should help:

http://stackoverflow.com/questions/188503/detecting-the-number-of-processors

James Black
A: 

exact duplicate :

uni processor or multi processor

Vijay Sarathi