getNumberOfCores.c


SUBMITTED BY: Guest

DATE: Dec. 4, 2013, 12:01 p.m.

FORMAT: C++

SIZE: 812 Bytes

HITS: 705

  1. #ifdef _WIN32
  2. #include <windows.h>
  3. #elif MACOS
  4. #include <sys/param.h>
  5. #include <sys/sysctl.h>
  6. #else
  7. #include <unistd.h>
  8. #endif
  9. int getNumberOfCores() {
  10. #ifdef WIN32
  11. SYSTEM_INFO sysinfo;
  12. GetSystemInfo(&sysinfo);
  13. return sysinfo.dwNumberOfProcessors;
  14. #elif MACOS
  15. int nm[2];
  16. size_t len = 4;
  17. uint32_t count;
  18. nm[0] = CTL_HW; nm[1] = HW_AVAILCPU;
  19. sysctl(nm, 2, &count, &len, NULL, 0);
  20. if(count < 1) {
  21. nm[1] = HW_NCPU;
  22. sysctl(nm, 2, &count, &len, NULL, 0);
  23. if(count < 1) { count = 1; }
  24. }
  25. return count;
  26. #else
  27. return sysconf(_SC_NPROCESSORS_ONLN);
  28. #endif
  29. }
  30. # example
  31. # printf("%x", getNumberOfCores());

comments powered by Disqus