Click here to Skip to main content
15,904,652 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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?
Posted
Updated 18-Dec-10 0:57am
v2
Comments
Ankur\m/ 18-Dec-10 6:54am    
I have deleted the other same post of yours. Please do not create more than one post for the same question. If you need to add.modify something to your question, use 'Improve Question' link below the question.
rupeshkp728 18-Dec-10 7:45am    
ok

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.
 
Share this answer
 
Comments
rupeshkp728 18-Dec-10 15:40pm    
Thanks Jim for the reply.
The thread function is getting completing execution in no time and so I am getting a count of one.
If we put the code inside the function in an infinite while loop then we properly get a thread count of 4(3 threads + main thread).
// 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)

before your ThreadProc returns :)
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900