views:

2247

answers:

2

I have some multi threaded code that makes one thread per core except I'm having troubles working out how to get the core count under windows.

For linux i have:

//from http://www.cplusplus.com/forum/unices/6544/
FILE * fp;
char res[128] = {0};
fp = popen("/bin/cat /proc/cpuinfo |grep -c '^processor'","r");
fread(res, 1, sizeof(res)-1, fp);
fclose(fp);

if (res)
 return atoi(res);

return 0;

but I can't find a way that works under windows. Whats the best way of getting the count under windows?

+2  A: 

Duplicate of Programmatically find the number of cores on a machine.

cletus
That didnt come up in my search. Thanks. :P
Lodle
I only found it because it came up on a Google search. The SO search seems to be... not great.
cletus
A: 

What threading library are you using?

Some libraries like OpenMP, will automatically create as many threads as you need to fill all your cores / CPUs.

Eclipse