CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4
  1. #1
    Join Date
    Jun 2010
    Posts
    4

    Monitor Process Thread

    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]);
    }
    }

    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?

  2. #2
    Join Date
    Oct 2009
    Posts
    577

    Smile Re: Monitor Process Thread

    Please use [c o d e] [/c o d e] tags or the code is hardly readable.

    In the ThreadProc I don't see an infinite loop or any wait operation. If so, the threads would end within very little time and only the main thread shows in the tools.

  3. #3
    Join Date
    Oct 2009
    Posts
    577

    Smile Re: Monitor Process Thread

    Note, your thread count must be 4 if all three additional threads were running.

    You could do a Sleep(20000); into the thread function and you should see the correct count for 20 seconds.

  4. #4
    Join Date
    Jun 2010
    Posts
    4

    Re: Monitor Process Thread

    Thanks Itsme for the reply.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured