Is there any way to find out the actual time spent by a thread inside the CPU using MFC/Win32? By actual time, I mean I don't want to count the time the thread spent in states other than "Running". I tried GetThreadTimes() method in Win32 SDK but couldn't get the proper values. I want to do this because I want to check effect of setting the ThreadPriority on its scheduling i.e. how much of a difference does it make if I set the thread priority to THREAD_PRIORITY_BELOW_NORMAL instead of THREAD_PRIORITY_NORMAL?
Here is the actual code:
#include<iostream>
#include <afxwin.h>
#include <afxmt.h>
#include <time.h>
bool bStop = false;
long k1 = 0;
long k2 = 0;
UINT run1(LPVOID param)
{
while(!bStop)
{
for(int i = 0; i < 10; ++i)
{
++k1;
}
}
return 0;
}
UINT run2(LPVOID param)
{
while(!bStop)
{
for(int i = 0; i < 10; ++i)
{
++k2;
}
}
return 0;
}
void main(int argc,char *argv[])
{
CWinThread* pThread1 = AfxBeginThread(&run1, NULL, THREAD_PRIORITY_NORMAL);
CWinThread* pThread2 = AfxBeginThread(&run2, NULL, THREAD_PRIORITY_BELOW_NORMAL );
Sleep(3 * 1000);
bStop = TRUE;
FILETIME f1,f2,f3,f4;
GetThreadTimes(*pThread1, &f1,&f2,&f3,&f4);
CTime t1(f4);
std::cout<<"K1="<<k1<<"\n";
std::cout<<"K2="<<k2<<"\n";
std::cout<<"Thread1 Time="<<t1.GetSecond()<<"sec\n";
std::cout<<"Exit\n";
}