测试CPU核心个数

//测试CPU核心个数

#if !defined (_WIN32) && !defined (_WIN64)

#define LINUX

#include <unistd.h>

#else

#define WINDOWS

#include <windows.h>

#endif

unsigned core_count()

{

  unsigned count = 1; // 至少一个

  #if defined (LINUX)

  count = sysconf(_SC_NPROCESSORS_CONF);

  #elif defined (WINDOWS)

  SYSTEM_INFO si;

  GetSystemInfo(&si);

  count = si.dwNumberOfProcessors;

  #endif

  return count;

}

#include <iostream>

int main()

{

  unsigned sz = core_count();

  std::cout << sz << (1 == sz ? "core" : "cores") << std::endl;

}

http://www.cppblog.com/Chipset/archive/2009/02/20/74403.html

你可能感兴趣的:(cpu)