Windows下擷取邏輯cpu數量和cpu核心數量(用GetLogicalProcessorInformation,從XP3才開始有的API)

來源:互聯網
上載者:User

標籤:mat   name   ase   data-   malloc   attribute   proc   mod   orm   

代碼可在Windows NT下正常運行

具體API說明請參照如下文檔:

GetLogicalProcessorInformation

點擊開啟連結

點擊開啟連結

點擊開啟連結

 

 

[html] view plain copy  
  1. typedef BOOL (WINAPI *LPFN_GLPI)(PSYSTEM_LOGICAL_PROCESSOR_INFORMATION, PDWORD);  
  2.   
  3. DWORD CountSetBits(ULONG_PTR bitMask)  
  4. {  
  5.     DWORD LSHIFT = sizeof(ULONG_PTR)*8 - 1;  
  6.     DWORD bitSetCount = 0;  
  7.     ULONG_PTR bitTest = (ULONG_PTR)1 << LSHIFT;      
  8.     DWORD i;  
  9.       
  10.     for (i = 0; i <= LSHIFT; ++i)  
  11.     {  
  12.         bitSetCount += ((bitMask & bitTest)?1:0);  
  13.         bitTest/=2;  
  14.     }  
  15.   
  16.     return bitSetCount;  
  17. }  
  18. LPFN_GLPI glpi;  
  19. glpi = (LPFN_GLPI) GetProcAddress(GetModuleHandle(TEXT("kernel32")),"GetLogicalProcessorInformation");  
  20. if (NULL == glpi)   
  21. {  
  22.     printf("GetLogicalProcessorInformation is not supported.\n");  
  23. }  
  24. BOOL done = FALSE;  
  25. PSYSTEM_LOGICAL_PROCESSOR_INFORMATION buffer = NULL;  
  26. PSYSTEM_LOGICAL_PROCESSOR_INFORMATION ptr = NULL;  
  27. DWORD returnLength = 0;  
  28. while (!done)  
  29. {  
  30.     DWORD rc = glpi(buffer, &returnLength);  
  31.   
  32.     if (FALSE == rc)   
  33.     {  
  34.         if (GetLastError() == ERROR_INSUFFICIENT_BUFFER)   
  35.         {  
  36.             if (buffer)   
  37.                 free(buffer);  
  38.   
  39.             buffer = (PSYSTEM_LOGICAL_PROCESSOR_INFORMATION)malloc(returnLength);  
  40.   
  41.             if (NULL == buffer)   
  42.             {  
  43.                 printf("Error: Allocation failure\n");  
  44.                 return (2);  
  45.             }  
  46.         }   
  47.         else   
  48.         {  
  49.             printf("Error %d\n", GetLastError());  
  50.             return (3);  
  51.         }  
  52.     }   
  53.     else  
  54.     {  
  55.         done = TRUE;  
  56.     }  
  57. }  
  58.   
  59. ptr = buffer;  
  60. DWORD byteOffset = 0;  
  61. DWORD logicalProcessorCount = 0;  
  62. DWORD processorCoreCount = 0;  
  63.   
  64. while (byteOffset + sizeof(SYSTEM_LOGICAL_PROCESSOR_INFORMATION) <= returnLength)   
  65. {  
  66.     switch (ptr->Relationship)   
  67.     {  
  68.         case RelationProcessorCore:  
  69.             processorCoreCount++;  
  70.             logicalProcessorCount += CountSetBits(ptr->ProcessorMask);  
  71.             break;  
  72.     }  
  73.     byteOffset += sizeof(SYSTEM_LOGICAL_PROCESSOR_INFORMATION);  
  74.     ptr++;  
  75. }  
  76. printf("logical:%d  core:%d\n", logicalProcessorCount, processorCoreCount);  

http://blog.csdn.net/tobacco5648/article/details/22201169

Windows下擷取邏輯cpu數量和cpu核心數量(用GetLogicalProcessorInformation,從XP3才開始有的API)

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.