// Cpucurve. cpp: defines the entry point for the console application.
//
# Include "stdafx. H"
# Include <assert. h>
# Include <iostream>
# Include <windows. h>
# Include <math. h>
Using namespace STD;
# Define m_pi 3.1415
# Define threadcnt 4
Double runtime = 0.0; // total execution time: divided into sleep and do-something
DWORD winapi threadfunc (lpvoid PARAM );
Void work (float C );
Void corefunc ();
Int _ tmain (INT argc, _ tchar * argv [])
{
DWORD corecnt = 0;
Handle * hthread;
Double timecnt (0.0 );
Large_integer lfreqstart, lfreqend, lfreqcnt;
Double temp (1, 0.0 );
// Obtain the maximum execution Duration: inaccurate
While (ABS (timecnt <m_pi ))
{
: Queryperformancecounter (& lfreqstart );
Work (1 + sin (timecnt )));
: Queryperformancecounter (& lfreqend );
: Queryperformancefrequency (& lfreqcnt );
Runtime = max (runtime, (double) (lfreqend. quadpart-lfreqstart. quadpart)/(double) (lfreqcnt. quadpart ));
Timecnt ++ = 0.1;
}
Runtime + = 0.1; // Error Calibration: prevents Overflow
System_info sysinfo;
: Getsysteminfo (& sysinfo );
Corecnt = sysinfo. dwnumberofprocessors;
Hthread = new handle [corecnt];
Memset (hthread, null, sizeof (handle) * corecnt );
While (corecnt --)
{
Hthread [corecnt] = createthread (null, 0, threadfunc, null, 0, null );
}
Dword dr = waitformultipleobjects (threadcnt, hthread, false, infinite );
Switch (DR)
{
Case wait_object_0:
Cout <"Thread 0 return" <Endl;
Break;
Case wait_object_0 + 1:
Cout <"thread 1 return" <Endl;
Break;
Case wait_object_0 + 2:
Cout <"thread 2 Return" <Endl;
Break;
Case wait_object_0 + 3:
Cout <"thread 3 return" <Endl;
Break;
}
Return 0;
}
DWORD winapi threadfunc (lpvoid PARAM)
{
Corefunc ();
Return 1;
}
Void corefunc ()
{
Double timecnt = 0.0; // time axis
Large_integer ltemp = {0 };
Double St, ET, FC, exetime; // start, end, counter, CPU usage time
Queryperformancefrequency (& ltemp );
Fc = double (ltemp. quadpart );
DWORD sleeptime (0 );
While (1)
{
// Do-something
Queryperformancecounter (& ltemp); // start time
St = double (ltemp. quadpart );
Work (sin (timecnt) + 1 );
Queryperformancecounter (& ltemp); // timing ends
ET = double (ltemp. quadpart );
Exetime = (ET-St)/FC; // obtain the actual CPU usage time
<"Exetime =" <exetime * 1000 <Endl;
If (ABS (runtime-exetime) <0.01)
Sleeptime = 0;
Else
Sleeptime = (DWORD) (runtime-exetime) * 1000 );
Cout <"sleeptime =" <sleeptime <Endl;
Sleep (sleeptime );
Timecnt ++ = 0.5;
}
}
Void work (float C)
{
Int Ic = (INT) (C * 10000*20 );
While (IC --> 0)
{
For (Int J = 1; j <1000; j ++ );
}
}