Re: Getting the number of CPUs?
Re: Getting the number of CPUs?
- Subject: Re: Getting the number of CPUs?
- From: Christopher Sean Morrison <email@hidden>
- Date: Tue, 7 Aug 2001 16:53:45 -0400 (EDT)
On Tue, 7 Aug 2001, [iso-8859-1] Stiphane Sudre wrote:
-On mardi, ao{t 7, 2001, at 07:52 PM, Ken Tabb wrote:
-
-> Hi,
-> is there any Cocoa-y way of getting the number of CPUs on the host? I'm
-> aiming to use this as part of a thread-spawning decision process (so
-> that
-> a single processor machine doesn't get twice as many threads as it needs
-> to do the job).
Dunno about the Cocoa-y way, but here is a Unix-y way of doing it that'll
work in cocoa:
(conversion to obj-c call is left as an exercise to the reader.. ;)
/** begin example **/
#include <stdio.h>
#include <sys/param.h>
#include <sys/sysctl.h>
int bu_avail_cpus() {
int mib[2];
size_t len;
int maxproc = 1;
mib[0] = CTL_HW;
mib[1] = HW_NCPU;
len = sizeof(maxproc);
if (sysctl(mib, 2, &maxproc, &len, NULL, NULL == -1)) {
perror("could not determine number of cpus available");
}
return maxproc;
}
/** end example **/
See the sysctl manpage from a terminal window (man 3 sysctl) for more
details than you'll care to know about things you can poke your system
for. The above is even "slightly" portable. Kudos to Apple/Mach kernel
coders for delivering a posix implementation (including a very useful
sysctl) that is "more-up-to-date-than-usual".
Cheers!
Christopher Sean Morrison
email@hidden