zoukankan      html  css  js  c++  java
  • Windows下获取逻辑cpu数量和cpu核数量

    代码可在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. ");  
    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 ");  
    44.                 return (2);  
    45.             }  
    46.         }   
    47.         else   
    48.         {  
    49.             printf("Error %d ", 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 ", logicalProcessorCount, processorCoreCount);  

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

  • 相关阅读:
    Nginx的访问控制
    远程登录
    Linux的网络命令
    laravel-collect
    laravel-model
    laravel-Macroable
    laravel-容器
    机器学习-Logisitic回归
    机器学习-多变量线性回归
    算法笔记-分支界限法
  • 原文地址:https://www.cnblogs.com/findumars/p/6351226.html
Copyright © 2011-2022 走看看