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
2010-04-11 23:59:40
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
2010-04-12 07:03:38
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
2010-04-12 00:51:00
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
2010-04-12 00:04:33