线程数监视器 [英] thread count monitor

查看:116
本文介绍了线程数监视器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在代码中创建了3个线程:

I have created 3 threads in my code:

#include <windows.h>
#include <strsafe.h>

#define MAX_THREADS 3
#define BUF_SIZE 255

typedef struct _MyData {
    int val1;
    int val2;
} MYDATA, *PMYDATA;

DWORD WINAPI ThreadProc( LPVOID lpParam ) 
{ 
    HANDLE hStdout;
    PMYDATA pData;

    TCHAR msgBuf[BUF_SIZE];
    size_t cchStringSize;
    DWORD dwChars;

    hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
    if( hStdout == INVALID_HANDLE_VALUE )
        return 1;

    // Cast the parameter to the correct data type.

    pData = (PMYDATA)lpParam;

    // Print the parameter values using thread-safe functions.

    StringCchPrintf(msgBuf, BUF_SIZE, TEXT("Parameters = %d, %d\n"), 
        pData->val1, pData->val2); 
    StringCchLength(msgBuf, BUF_SIZE, &cchStringSize);
    WriteConsole(hStdout, msgBuf, cchStringSize, &dwChars, NULL);

    // Free the memory allocated by the caller for the thread 
    // data structure.

    HeapFree(GetProcessHeap(), 0, pData);

    return 0; 
} 
 
void main()
{
    PMYDATA pData;
    DWORD dwThreadId[MAX_THREADS];
    HANDLE hThread[MAX_THREADS]; 
    int i;

    // Create MAX_THREADS worker threads.

    for( i=0; i<MAX_THREADS; i++ )
    {
        // Allocate memory for thread data.

        pData = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY,
                sizeof(MYDATA));

        if( pData == NULL )
            ExitProcess(2);

        // Generate unique data for each thread.

        pData->val1 = i;
        pData->val2 = i+100;

        hThread[i] = CreateThread( 
            NULL,              // default security attributes
            0,                 // use default stack size  
            ThreadProc,        // thread function 
            pData,             // argument to thread function 
            0,                 // use default creation flags 
            &dwThreadId[i]);   // returns the thread identifier 
 
        // Check the return value for success. 
 
        if (hThread[i] == NULL) 
        {
            ExitProcess(i);
        }
    }

    // Wait until all threads have terminated.

    WaitForMultipleObjects(MAX_THREADS, hThread, TRUE, INFINITE);

    // Close all thread handles upon completion.

    for(i=0; i<MAX_THREADS; i++)
    {
        CloseHandle(hThread[i]);
    }
}


但是在性能监视器,进程浏览器之类的工具中,它仅显示一个线程数.
甚至在创建了3个线程之后,这些工具仍只显示一个线程的原因可能是什么?
为什么我的线程数不为3?


But in tools like performance monitor, process explorer it show only one thread count.
What may be the reason that even after creating 3 threads these tools are showing only one?
Why I am not getting the thread count of 3?

推荐答案

3个线程没有执行任何需要花费大量时间的事情,因此可能因为它们启动和消失太快,以至于任何性能工具都无法注意到它们曾经被创建过.

我在计算机上通过在线程proc内放置Sleep(1000);进行了检查.没有它,Process Explorer不会注意到这三个线程,有了它们,它们的寿命就足以显示出来,然后迅速从线程列表中消失.
The 3 threads don''t do anything that takes a significant amount of time so it''s probably because they start and die too quickly for any performance tool to notice they were ever created.

I checked this on my machine by putting a Sleep(1000); inside the thread proc. Without it Process Explorer doesn''t notice the three threads, with it they live just long enough to show up and then quickly disappear from the thread list.


//为什么我没有得到线程数为3?

只需设置
的通话
// Why I am not getting the thread count of 3?

Just set the call of
::MessageBox(NULL, _T("Please click OK to end this thread"), _T("Done"), MB_OK)


在ThreadProc返回之前:)


before your ThreadProc returns :)


这篇关于线程数监视器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆