C + + Write code to see the number of CPUs, the number of single CPU cores, the number of individual CPU threads

Source: Internet
Author: User

Number of CPUs: This refers to the number of physical CPUs, that is, in the DOS interface, when entering systeminfo, display the number of processors in the message

CPU cores: This refers to the number of CPU cores, that is, in the DOS interface, when you enter WMIC, the numberofcores displayed when you type CPU get *

CPU Threads: This is referred to in the DOS interface, when you enter WMIC, the numberoflogicalprocessors displayed when you type CPU get *

Normally, our CPU is talking about a few cores, such as dual core, quad core, and so on, which is the core, and now there is a technology is hyper-Threading technology, which is a core, two threads, and normally, a core has only one thread;

In addition to the DOS interface you can see the number of CPU cores (as described above), you can also see in the Windows Interface Task Manager, the performance of the CPU display of the number of columns, that is, the number of columns can be seen from the number of CPUs.

In C + + writing to view CPU-related information, select GetLogicalProcessorInformation to get some information about the CPU, the code is as follows:

1#include <windows.h>2#include <malloc.h>3#include <stdio.h>4#include <tchar.h>5 6 #if(_win32_winnt < 0x0600)//[zyl910] Low version of the Windows SDK does not define constants such as Relationprocessorpackage7 #defineRelationprocessorpackage 38 #defineRelationgroup 49 #endifTen //[zyl910] name of the Logical_processor_relationship enumeration One ConstLPTSTR names_logical_processor_relationship[] = { A_t ("Relationprocessorcore") -, _t ("Relationprocessorpackage") - }; the  -typedef BOOL (WINAPI *Lpfn_glpi) ( - Psystem_logical_processor_information, - Pdword); +  -  + //Helper function to count set bits in the processor mask. A DWORD countsetbits (ulong_ptr bitmask) at { -DWORD LShift =sizeof(ULONG_PTR) *8-1; -DWORD Bitsetcount =0; -Ulong_ptr Bittest = (ULONG_PTR)1<<LShift;  - DWORD i; -      in      for(i =0; I <= LShift; ++i) -     { toBitsetcount + = ((Bitmask & bittest)?1:0); +Bittest/=2; -     } the  *     returnBitsetcount; $ }Panax Notoginseng  - intMain () the { + Lpfn_glpi GLPI; ABOOL done =FALSE; thePsystem_logical_processor_information buffer =NULL; +Psystem_logical_processor_information ptr =NULL; -DWORD returnlength =0; $DWORD Logicalprocessorcount =0; $DWORD Processorcorecount =0; -DWORD Processorpackagecount =0; -DWORD Byteoffset =0; the  -Glpi =(LPFN_GLPI) GetProcAddress (WuyiGetModuleHandle (TEXT ("kernel32")), the                             "getlogicalprocessorinformation"); -     if(NULL = =Glpi) Wu     { -printf"\ngetlogicalprocessorinformation is not supported.\n"); About         return(1); $     } -  -      while(!Done ) -     { ADWORD rc = Glpi (buffer, &returnlength); +  the         if(FALSE = =RC) -         { $             if(GetLastError () = =error_insufficient_buffer) the             { the                 if(buffer) the                      Free(buffer); the  -Buffer = (psystem_logical_processor_information)malloc( in returnlength); the  the                 if(NULL = =buffer) About                 { theprintf"\nerror:allocation failure\n"); the                     return(2); the                 } +             }  -             Else  the             {Bayiprintf"\nerror%d\n", GetLastError ()); the                 return(3); the             } -         }  -         Else the         { theDone =TRUE; the         } the     } -  thePTR =buffer; the  the     if(true)//[zyl910] Displays details of the system_logical_processor_information structure94     { theDWORD cnt = returnlength/sizeof(system_logical_processor_information);//calculating the number of system_logical_processor_information structures the          for(DWORD i=0; i<cnt; ++i) the         {98printf"system_logical_processor_information[%d]\n"+ N); printf"\t.processormask:\t0x%.16i64x\t//%i64d\n", (UINT64) ptr[i]. Processormask, (UINT64) ptr[i]. Processormask); Aboutprintf"\t.relationship:\t%d\t//%s\n", Ptr[i]. Relationship, Names_logical_processor_relationship[max (0, Min (Ptr[i]. Relationship, Relationgroup)]); -              for(intj=0; j<2; ++J) printf ("\t.reserved[%d]:\t//0x%.16i64x\t%i64d\n", J, (UINT64) ptr[i]. RESERVED[J], (UINT64) ptr[i]. RESERVED[J]);101         }102     }103 104      while(Byteoffset +sizeof(system_logical_processor_information) <=returnlength) the     {106         Switch(ptr->relationship)107         {108          CaseRelationprocessorcore:109processorcorecount++; the 111             //A hyperthreaded Core supplies more than one logical processor. theLogicalprocessorcount + = Countsetbits (ptr->processormask);113              Break; the          CaseRelationprocessorpackage: the             //Logical Processors share a physical package. theprocessorpackagecount++;117              Break;118         }119Byteoffset + =sizeof(system_logical_processor_information); -ptr++;121     }122 123printf"\ngetlogicalprocessorinformation results:\n");124printf"Number of physical processor packages:%d\n",  the processorpackagecount);126printf"Number of processor cores:%d\n", processorcorecount);127printf"Number of logical processors:%d\n", logicalprocessorcount); -     129      Free(buffer); the 131     return 0; the}

C + + Write code to see the number of CPUs, the number of single CPU cores, the number of individual CPU threads

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.