• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: Getting the number of CPUs?
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

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


  • Prev by Date: Anyone have a fix?: How to make NSButton not invert (gray) when tracking mouse?
  • Next by Date: CGPDFContext
  • Previous by thread: Re: Getting the number of CPUs?
  • Next by thread: Re: Retained items and autorelease pool
  • Index(es):
    • Date
    • Thread