5

I am writing some code with dispatch_async and get different results on an iphone 4s and ipad 1st gen.

I am wondering if it is due to the number of cores the CPU has. Is it possible to detect the number of cores or CPU type of an iOS device at runtime so I can dispatch_async on the 4s, but not on the ipad?

Rob
  • 415,655
  • 72
  • 787
  • 1,044
some_id
  • 29,466
  • 62
  • 182
  • 304

1 Answers1

8

Here's the code to detect the number of cores on an iOS device:

#include <sys/sysctl.h>

unsigned int countCores()
{
    size_t len;
    unsigned int ncpu;

    len = sizeof(ncpu);
    sysctlbyname ("hw.ncpu",&ncpu,&len,NULL,0);

    return ncpu;
}

In addition to that, you can check against the [[UIDevice currentDevice] userInterfaceIdiom] to determine if the device is an iPhone or an iPad. Like this:

if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPad) {
    NSLog(@"iPad");
}
else {
    NSLog(@"iPhone");
}

Reference

Community
  • 1
  • 1
Simon Germain
  • 6,834
  • 1
  • 27
  • 42