Number of logical CPUs and CPU cores obtained under Windows (with GetLogicalProcessorInformation, API starting from XP3)

Source: Internet
Author: User

Code can run correctly under Windows NT

Please refer to the following documentation for specific API descriptions:

GetLogicalProcessorInformation

Click to open link

Click to open link

Click to open link

[HTML]View PlainCopy 
  1. typedef BOOL (WINAPI *lpfn_glpi) (psystem_logical_processor_information, Pdword);
  2. DWORD countsetbits (ULONG_PTR bitmask)
  3. {
  4. DWORD lshift = sizeof (ULONG_PTR) *8-1;
  5. DWORD bitsetcount = 0;
  6. Ulong_ptr bittest = (ulong_ptr) 1 << LShift;
  7. DWORD i;
  8. for (i = 0; I <= lshift; ++i)
  9. {
  10. Bitsetcount + = ((Bitmask & Bittest) 1:0);
  11. bittest/=2;
  12. }
  13. return bitsetcount;
  14. }
  15. Lpfn_glpi GLPI;
  16. Glpi = (LPFN_GLPI) GetProcAddress (GetModuleHandle (TEXT ("kernel32")), "getlogicalprocessorinformation");
  17. if (NULL = = Glpi)
  18. {
  19. printf ("GetLogicalProcessorInformation is not supported.\n");
  20. }
  21. BOOL Done = FALSE;
  22. Psystem_logical_processor_information buffer = NULL;
  23. Psystem_logical_processor_information ptr = NULL;
  24. DWORD returnlength = 0;
  25. while (!done)
  26. {
  27. DWORD rc = glpi (buffer, &returnlength);
  28. if (FALSE = = RC)
  29. {
  30. if (GetLastError () = = Error_insufficient_buffer)
  31. {
  32. if (buffer)
  33. Free (buffer);
  34. buffer = (psystem_logical_processor_information) malloc (returnlength);
  35. if (NULL = = buffer)
  36. {
  37. printf ("Error:allocation failure\n");
  38. return (2);
  39. }
  40. }
  41. Else
  42. {
  43. printf ("Error%d\n", GetLastError ());
  44. return (3);
  45. }
  46. }
  47. Else
  48. {
  49. Done = TRUE;
  50. }
  51. }
  52. ptr = buffer;
  53. DWORD byteoffset = 0;
  54. DWORD logicalprocessorcount = 0;
  55. DWORD processorcorecount = 0;
  56. while (Byteoffset + sizeof (system_logical_processor_information) <= returnlength)
  57. {
  58. Switch (ptr->relationship)
  59. {
  60. Case Relationprocessorcore:
  61. processorcorecount++;
  62. Logicalprocessorcount + = countsetbits (ptr->processormask);
  63. Break
  64. }
  65. Byteoffset + = sizeof (system_logical_processor_information);
  66. ptr++;
  67. }
  68. printf ("logical:%d core:%d\n", Logicalprocessorcount, Processorcorecount);

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

Number of logical CPUs and CPU cores obtained under Windows (with GetLogicalProcessorInformation, API starting from XP3)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.