Geesu
Geesu

Reputation: 6008

Determining the number of available cores on a machine?

I want my software to create one thread per core, obviously different Macs have a different number of cores.

Does anyone know how to (programmatically, via Cocoa) determine the number of cores?

Upvotes: 4

Views: 2138

Answers (2)

JWWalker
JWWalker

Reputation: 22707

[[NSProcessInfo processInfo] processorCount]

Upvotes: 26

Jano
Jano

Reputation: 63667

See How do I detect a dual core CPU on iOS? It works in OS X too.

unsigned int countCores()
{
    size_t len;
    unsigned int ncpu;
    len = sizeof(ncpu);
    sysctlbyname ("hw.ncpu",&ncpu,&len,NULL,0);
    return ncpu;
}

Upvotes: 2

Related Questions