| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1414 人关注过本帖
标题:有关window编程多线程
只看楼主 加入收藏
九天冥盟
Rank: 2
等 级:论坛游民
威 望:1
帖 子:77
专家分:20
注 册:2015-7-14
结帖率:36%
收藏
已结贴  问题点数:4 回复次数:1 
有关window编程多线程
这是Windows核心编程中的例子,我照打了一遍,就是添加了需要测试的任务(sleep(1000)),为什么输出结果都是0?求请教.

__int64 FileTimeToQuadWord(PFILETIME pft)
{
    return (Int64ShllMod32(pft->dwHighDateTime,32)| pft->dwLowDateTime);
}

void GetTaskTime()
{
    FILETIME ftKernelTimeStart,ftKernelTimeEnd;
    FILETIME ftUserTimeStart,ftUserTimeEnd;
    FILETIME ftDummy;
    __int64 qwKernelTimeElapsed,qwUserTimeElapsed,qwTotalTimeElapsed;
    GetThreadTimes(GetCurrentThread(),&ftDummy,&ftDummy,&ftKernelTimeStart,&ftUserTimeStart);
    //执行的任务
    Sleep(1000);
   
    GetThreadTimes(GetCurrentThread(),&ftDummy,&ftDummy,&ftKernelTimeEnd,&ftUserTimeEnd);
    qwKernelTimeElapsed=FileTimeToQuadWord(&ftKernelTimeEnd)-FileTimeToQuadWord(&ftKernelTimeStart);
    qwUserTimeElapsed=FileTimeToQuadWord(&ftUserTimeEnd)-FileTimeToQuadWord(&ftUserTimeStart);
    qwTotalTimeElapsed=qwKernelTimeElapsed+qwUserTimeElapsed;
    _tprintf(_T("kernel: %d,uer: %d, the total time elapsed is: %d"),qwKernelTimeElapsed,qwUserTimeElapsed,qwTotalTimeElapsed);
}

int _tmain(int argc, _TCHAR* argv[])
{
    GetTaskTime();
    system("pause");
    return 0;
}
搜索更多相关主题的帖子: window 编程 多线程 任务 int 
2018-12-16 21:20
rjsp
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
等 级:版主
威 望:528
帖 子:9031
专家分:54061
注 册:2011-1-18
收藏
得分:4 
随手改的,可能代码不严格

程序代码:
#include <windows.h>
#include <stdio.h>

bool NotSleep( DWORD dwMilliseconds )
{
    LARGE_INTEGER frequency, starttime;
    if( !QueryPerformanceFrequency(&frequency) )
        return false;
    if( !QueryPerformanceCounter(&starttime) )
        return false;
    starttime.QuadPart += dwMilliseconds*frequency.QuadPart/1000;

    for( LARGE_INTEGER curtime; ; )
    {
        if( !QueryPerformanceCounter(&curtime) )
            return false;
        if( curtime.QuadPart >= starttime.QuadPart )
            break;
    }
    return true;
}

bool GetThreadTimeU64( unsigned long long& KernelTime, unsigned long long& UserTime )
{
    FILETIME ct, et, kt, ut;
    if( !GetThreadTimes(GetCurrentThread(),&ct,&et,&kt,&ut) )
        return false;

    KernelTime = (kt.dwHighDateTime+0ull)<<32 | kt.dwLowDateTime;
    UserTime = (ut.dwHighDateTime+0ull)<<32 | ut.dwLowDateTime;
    return true;
}

void GetTaskTime()
{
    unsigned long long KernelTimeStart, UserTimeStart;
    GetThreadTimeU64( KernelTimeStart, UserTimeStart );

    NotSleep( 1000 );

    unsigned long long KernelTimeEnd, UserTimeEnd;
    GetThreadTimeU64( KernelTimeEnd, UserTimeEnd );

    unsigned long long KernelTimeElapsed = KernelTimeEnd - KernelTimeStart;
    unsigned long long UserTimeElapsed = UserTimeEnd - UserTimeStart;
    unsigned long long TotalTimeElapsed = KernelTimeElapsed + UserTimeElapsed;

    printf( "kernel: %llu, user: %llu, the total time elapsed is: %llu\n", KernelTimeElapsed, UserTimeElapsed, TotalTimeElapsed );
}

int main( void )
{
    GetTaskTime();
    return 0;
}

2018-12-17 09:38
快速回复:有关window编程多线程
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.022306 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved