Reprinted please indicate the source, original address: http://blog.csdn.net/morewindows/article/details/8678396
Welcome to Weibo: http://weibo.com/MoreWindows
Windows CPU Memory Network Performance Statistics Article 4 CPU multi-core CPU core usage C ++
Http://blog.csdn.net/morewindows/article/details/8678396
This article Windows system CPU Memory Network Performance Statistics Fourth Article CPU multi-core CPU core usage C ++ (http://blog.csdn.net/morewindows/article/details/8678396) this section describes how to reference the C # code in VC ++ to collect statistics on the usage of each core of a multi-core CPU.
Windows system CPU Memory Network Performance Statistics blog directory:
1. Windows system CPU Memory Network Performance Statistics Article 1 memory
Http://blog.csdn.net/morewindows/article/details/8459219
2. Windows system CPU Memory Network Performance Statistics Article 2 Overall CPU usage
Http://blog.csdn.net/morewindows/article/details/8678359
3. Windows system CPU Memory Network Performance Statistics Article 3 CPU multi-core CPU core usage C #
Http://blog.csdn.net/morewindows/article/details/8678382
4. Windows system CPU Memory Network Performance Statistics Article 4 CPU multi-core CPU core usage C ++
Http://blog.csdn.net/morewindows/article/details/8678396
The first is the C # code. Note that this is a "C # class library" project, in which a cshapecpuuserate class is completed. The getcpueverycoreuserate function of this class returns a string containing the utilization of each CPU core, for example, if the usage of one core of a dual-core CPU is 3% and that of another core is 5%, "3, 5" is returned ".
// Windows system CPU Memory Network Performance Statistics Fourth Article CPU multi-core CPU core usage C ++ // http://blog.csdn.net/morewindows/article/details/8678396using system; using system. collections. generic; using system. LINQ; using system. text; using system. diagnostics; namespace cshapecpuuseratedll {public class cshapecpuuserate {public int initialize () {try {m_ncpucorenumber = system. environment. processorcount; m_pfcounters = new performancecounter [m_ncpucorenumber]; for (INT I = 0; I <m_ncpucorenumber; I ++) {m_pfcounters [I] = new performancecounter ("processor ", "% processor time", I. tostring () ;}} catch (system. exception e) {return 0;} return 1;} public int getcpucorenumber () {return m_ncpucorenumber;} Public String getcpueverycoreuserate () {stringbuilder strbuild = new stringbuilder (); float Frate = m_pfcounters [0]. nextvalue (); int nrate = convert. toint32 (Frate); strbuild. append (nrate. tostring (); For (INT I = 1; I <m_ncpucorenumber; I ++) {Frate = m_pfcounters [I]. nextvalue (); nrate = convert. toint32 (Frate); strbuild. append ("," + nrate. tostring ();} return strbuild. tostring ();} private performancecounter [] m_pfcounters; private int m_ncpucorenumber ;}}
How to call C # code in C ++ can refer to C ++ call C # code through DLL (http://blog.csdn.net/morewindows/article/details/8678431 ).
// Windows system CPU Memory Network Performance Statistics Fourth Article CPU multi-core CPU core usage C ++ // http://blog.csdn.net/morewindows/article/details/8678396//#using "cshapecpuuseratedll \ bin \ debug \ cshapecpuuseratedll. DLL "# using" cshapecpuuseratedll \ bin \ release \ cshapecpuuseratedll. DLL "# include <windows. h> # include <stdio. h> # include <conio. h> # include <string. h> using namespace cshapecpuuseratedll; int main () {PR INTF ("Windows system CPU Memory Network Performance Statistics Fourth Article CPU multi-core CPU core usage C ++ \ n"); printf ("-http://blog.csdn.net/morewindows/article/details/8678396-\ n "); printf ("-- by morewindows (http://blog.csdn.net/MoreWindows) -- \ n"); cshapecpuuserate ^ cpuuserate = gcnew cshapecpuuserate; If (! Cpuuserate-> initialize () {printf ("error! \ N "); getch (); Return-1;} else {printf (" CPU in the system is % d core CPU \ n ", cpuuserate-> getcpucorenumber ()); while (true) {sleep (1000); printf ("\ r current CPU core usage: % s", cpuuserate-> getcpueverycoreuserate ());}} return 0 ;}
The program running result is as follows:
This method of calling the C # code through C ++ to obtain the CPU usage of each core is not very good, and later look for information to see how to directly obtain the CPU usage of each core in C ++, thank you for your advice.
Reprinted please indicate the source, original address: http://blog.csdn.net/morewindows/article/details/8678396
Welcome to Weibo: http://weibo.com/MoreWindows