Detecting the number of CPUs
Posted: 18 Nov 2007, 13:31
In Linux and Mac OS X, I use the following function to find the number of available CPUs:
This seems to work fine, but I also need a similar function for Windows. After some Googling, I came up with the following function, but because I don't have any Windows computers, I can't test if it works. Does the following look correct?
The Min(<whatever>, 8) thing in the code is there simply because my program is currently limited to a maximum of 8 threads.
Tord
- Code: Select all
int cpu_count() {
return Min(sysconf(_SC_NPROCESSORS_ONLN), 8);
}
This seems to work fine, but I also need a similar function for Windows. After some Googling, I came up with the following function, but because I don't have any Windows computers, I can't test if it works. Does the following look correct?
- Code: Select all
int cpu_count() {
SYSTEM_INFO s;
GetSystemInfo(&s);
return Min(s.dwNumberOfProcessors, 8);
}
The Min(<whatever>, 8) thing in the code is there simply because my program is currently limited to a maximum of 8 threads.
Tord