分析多线程环境中的每个线程

发布于 2024-07-14 17:09:12 字数 1329 浏览 4 评论 0原文

有没有办法使用 MFC/Win32 找出 CPU 内线程实际花费的时间? 就实际时间而言,我的意思是我不想计算线程在“运行”以外的状态下花费的时间。 我尝试了 Win32 SDK 中的 GetThreadTimes() 方法,但无法获得正确的值。 我想这样做是因为我想检查设置 ThreadPriority 对其调度的影响,即如果我将线程优先级设置为 THREAD_PRIORITY_BELOW_NORMAL 而不是 THREAD_PRIORITY_NORMAL,会有多大差异?

这是实际的代码:

#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";
}

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";
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

拍不死你 2024-07-21 17:09:13

GetThreadTimes() 将为您提供正确的值。 问题是它以 FILETIME 结构给出。 您必须将 FILETIME 结构转换为易于向用户显示的时间。 您可以使用 API FileTimeToSystemTime

SYSTEMTIME stUTC;        
FileTimeToSystemTime(&f4, &stUTC);
std::cout<<"Thread1: Hour:"<<stUTC.wHour<<" Min:"<<stUTC.wMinute<<" Sec:"<<stUTC.wSecond<<" millSec:"<<stUTC.wMilliseconds<<std::endl;

来 完成此操作:我的机器的输出是: Thread1: Hour:0 Min:0 Sec:3 millSec:0

请忽略年\日详细信息,因为 FILETIME 包含表示 100 纳秒间隔数的 64 位值自 1601 年 1 月 1 日(世界标准时间)起。

不要忘记添加内核时间和用户时间以获得总时间。

GetThreadTimes() will give you the correct values. The problem is it gives it in FILETIME structure. You will have to convert FILETIME structure into a time that is easy to display to a user. You can do it with API FileTimeToSystemTime :

SYSTEMTIME stUTC;        
FileTimeToSystemTime(&f4, &stUTC);
std::cout<<"Thread1: Hour:"<<stUTC.wHour<<" Min:"<<stUTC.wMinute<<" Sec:"<<stUTC.wSecond<<" millSec:"<<stUTC.wMilliseconds<<std::endl;

In my machine the output was : Thread1: Hour:0 Min:0 Sec:3 millSec:0

Please ignore the year\day details as FILETIME Contains a 64-bit value representing the number of 100-nanosecond intervals since January 1, 1601 (UTC).

Don't forget to add kernerl time and user time to get the total time.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文