CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 1 of 3 123 LastLast
Results 1 to 15 of 37
  1. #1
    Join Date
    Apr 2005
    Posts
    75

    What is the maximum number of threads per process in Windows

    the subject says it all

  2. #2
    Join Date
    Feb 2005
    Location
    Normandy in France
    Posts
    4,590

    Re: What is the maximum number of threads per process in Windows

    There is no official limit.
    The real limit is the total number of thread, and not the number of thread per process.
    The limit is defined by global system resources (like max available ram), or other limits specific to an OS, and undocumented.
    You must not take care about a such limitation.

    With my computer (K6-2 333 with 192 Mo RAM), i can create about 1500 threads for the whole system.
    But newer computers with Win XP can certainly have many, many more threads.

  3. #3
    Join Date
    Apr 2005
    Posts
    75

    Re: What is the maximum number of threads per process in Windows

    i have a probem with my binary tree that consists of 1024 threads ... utill i keep it under 1000 it works fine once i make more than a 1000 the program crashes... also i tried with a simple multithreaded program which only creates 1000 threas and keeps them working at the same time... the same thing happens

  4. #4
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    Re: What is the maximum number of threads per process in Windows

    I don't know anything about your design, but 1000+ threads is excessive. You may consider using thread pooling. Windows has a handy one for you with QueueUserWorkItem(...).

    Arjay

  5. #5
    Join Date
    Apr 2005
    Posts
    75

    Re: What is the maximum number of threads per process in Windows

    Quote Originally Posted by Arjay
    I don't know anything about your design, but 1000+ threads is excessive. You may consider using thread pooling. Windows has a handy one for you with QueueUserWorkItem(...).

    Arjay
    i use visual studio c++ and i can't find such thing as QueueUserWorkItem()

  6. #6
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    Re: What is the maximum number of threads per process in Windows

    Here you go: QueueUserWorkItem.


    Arjay

  7. #7
    Join Date
    Apr 2005
    Posts
    75

    Re: What is the maximum number of threads per process in Windows

    could you explain a bit more how this pooling works...I have to create a binary tree in which each node is a thread. That binary tree consists of about 1024 elements (calculating an integral). Once I build the tree the lowest children calculate a certain function and they send the result up to their parent who sums the results from its two children and sends it to its parent (each child was created by its parent).

  8. #8
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    Re: What is the maximum number of threads per process in Windows

    In your current code, you are creating a thread that runs a calculation, right? The difference in using a thread pool is that instead of each node creating its own thread, you would pass a function to QueueUserWorkItem and QueueUserWorkItem would create the necessary threads for you. If you post your thread proc code, I will show you how to use the api.

    Arjay

  9. #9
    Join Date
    Apr 2005
    Posts
    75

    Re: What is the maximum number of threads per process in Windows

    i pushed everything in one file for simplicity

    Code:
    #include <windows.h>
    #include <stdio.h>
    #include <cassert>
    #include <conio.h>
    #include <math.h>
    
    struct ThreadParam {
                double lUpper;
                double lLower;
                double lDeltaX;
    
    };
    
    DWORD WINAPI Integral(LPVOID param){
    
    	
    	HANDLE* ThreadHandles = new HANDLE[2];
    	float lSizeOfMyInterval;
            ThreadParam* pParam = (ThreadParam*) param; 
    	DWORD lResultFromChild1;
            DWORD lResultFromChild2;
    	DWORD lResultToSend;
        
    	float* pResultToSend = new float;
    	float* pResultFromChild1;
    	float* pResultFromChild2;
    	
            lSizeOfMyInterval = pParam->lUpper - pParam->lLower;
    
            if(lSizeOfMyInterval > pParam->lDeltaX){  
    											
    		ThreadParam* pParam1 = new ThreadParam; 
    		pParam1->lLower = pParam->lLower;
    		pParam1->lUpper = pParam->lLower + (lSizeOfMyInterval)/2;
    		pParam1->lDeltaX = pParam->lDeltaX;
                    ThreadHandles[0] = CreateThread(NULL, 0, Integral, (LPVOID)pParam1, 0, 0); 
    
    		ThreadParam* pParam2 = new ThreadParam; 
    		pParam2->lLower = pParam->lLower + (lSizeOfMyInterval)/2;
    		pParam2->lUpper = pParam->lUpper;
    		pParam2->lDeltaX = pParam->lDeltaX;
    		ThreadHandles[1] = CreateThread(NULL, 0, Integral, (LPVOID)pParam2, 0, 0); 
    
    		WaitForMultipleObjects(2, ThreadHandles, TRUE, INFINITE);
    		GetExitCodeThread(ThreadHandles[0],&lResultFromChild1);
    		GetExitCodeThread(ThreadHandles[1],&lResultFromChild2);
    		pResultFromChild1=(float *) lResultFromChild1;
    		pResultFromChild2=(float *) lResultFromChild2;
    		*pResultToSend=*pResultFromChild1 + *pResultFromChild2;
    		delete [] ThreadHandles;
    		delete pResultFromChild1;
    		delete pResultFromChild2;
    	}
        else{
    		*pResultToSend=(exp(-0.5 * pParam->lDeltaX) * (cos(pParam->lLower) * lSizeOfMyInterval) +
    						exp(-0.5*pParam->lDeltaX) * (cos(pParam->lUpper) * lSizeOfMyInterval));
    			
        }
    	lResultToSend=(DWORD) pResultToSend;	
    	ExitThread(lResultToSend);
    	return 0;
    }
    int main(int argc, char* argv[])
    {
     
        HANDLE* ThreadHandles = new HANDLE[1];
    
    	DWORD 	code1;
    	float* transfer; 
        
        ThreadParam* pParam = new ThreadParam;
        pParam->lUpper = 2*3.14;
        pParam->lLower = 0;
        pParam->lDeltaX = 0.015;
                  
    
        ThreadHandles[0] = CreateThread(NULL, 0, Integral, (LPVOID)pParam, 0, 0);
    
       
        WaitForSingleObject(ThreadHandles[0], INFINITE); 
    
        GetExitCodeThread(ThreadHandles[0],&code1);
        transfer=(float*) code1;
        printf(" %f ",*transfer);
        printf("press any key");
        getch();
    
        delete [] ThreadHandles;
    	return 0;
    }
    Last edited by raindog; May 2nd, 2005 at 04:09 PM.

  10. #10
    Join Date
    Apr 2005
    Posts
    75

    Re: What is the maximum number of threads per process in Windows

    the problem occurs when i put the deltax to 0.01 as the assignment requires

  11. #11
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    Re: What is the maximum number of threads per process in Windows

    I took the liberty of changing the code slightly:
    1. Rather than using the thread return param to pass the result, the code now passes the result in the ThreadParam structure
    2. Since QueueUserWorkItem (QUWI) doesn't return a thread handle to wait on, we need to develop another method to signal when the work has been completed. I added a hEvent param to the ThreadParam structure to accomplish this.
    3. For debug purposes, I added a uCount param to the ThreadParam structure. This count is a global thread counter that is inc when the thread proc is entered and dec when exited.
    4. I removed all the 'new' heap allocations and replaced them with stack based allocations. This simplifies the code and makes cleanup easier. Usually you want to use heap based allocations when creating a thread to ensure the params don't go out of scope while the thread is executing. In this case, we ensure that we aren't going out of scope prematurely because we are waiting on both child events.
    This code seems to work fine with deltaX at 0.01. On my machine the max threads seem to peak at about 400 (which is better than the default QUWI thread pool count and much better than the nt max thread count).

    Arjay

    Code:
    #ifndef _WIN32_WINNT
    #define _WIN32_WINNT 0x0500
    #endif
    #include <tchar.h>
    #include <windows.h>
    #include <stdio.h>
    #include <cassert>
    #include <conio.h>
    #include <math.h>
     
    struct ThreadParam
    {
    double lUpper;
    double lLower;
    double lDeltaX;
    HANDLE hEvent;		 // Handle to wait on
    double dResultToSend; // Returns the result
    UINT* puCount;		 // Debug Only - tracks thread count
    };
     
    DWORD WINAPI Integral(LPVOID param)
    {
    double dSizeOfMyInterval = 0.0;
    ThreadParam* pParam = (ThreadParam*) param;
     
    // Debug spew -- increments thread count when enters fn;
    // decrements on exit
    (*(pParam->puCount))++;
    printf( _T("IN -- Thread Count: %d\n"), *(pParam->puCount) );
     
    dSizeOfMyInterval = pParam->lUpper - pParam->lLower;
     
    if(dSizeOfMyInterval > pParam->lDeltaX)
    {
    	ThreadParam tp1 = { pParam->lLower + (dSizeOfMyInterval) / 2,
    	 pParam->lLower,
    	 pParam->lDeltaX,
    	 ::CreateEvent( NULL, FALSE, FALSE, NULL ),
    	 0.0,
    	 pParam->puCount };
     
    	ThreadParam tp2 = { pParam->lUpper,
    	 pParam->lLower + (dSizeOfMyInterval)/2,
    	 pParam->lDeltaX,
    	 ::CreateEvent( NULL, FALSE, FALSE, NULL ),
    	 0.0,
    	 pParam->puCount };
    	HANDLE aHandles[] = { tp1.hEvent, tp2.hEvent };
     
    	// Queue each work item using the Windows thread pooling mechanism
    	::QueueUserWorkItem( Integral, &tp1, WT_EXECUTELONGFUNCTION );
    	::QueueUserWorkItem( Integral, &tp2, WT_EXECUTELONGFUNCTION );
     
    // Wait for both events to signal
    	WaitForMultipleObjects(sizeof(aHandles)/sizeof(HANDLE),
    	 &aHandles[0],
    	 TRUE,
    	 INFINITE);
     
    	// Cleanup both the handles
    	::CloseHandle( tp1.hEvent );
    	::CloseHandle( tp2.hEvent );
     
    	pParam->dResultToSend = tp1.dResultToSend + tp2.dResultToSend;
    }
    else
    {
    	pParam->dResultToSend 
    	 = (exp(-0.5 * pParam->lDeltaX)
    		* (cos(pParam->lLower)
    		* dSizeOfMyInterval)
    	 + exp(-0.5*pParam->lDeltaX)
    		* (cos(pParam->lUpper)
    		* dSizeOfMyInterval));
    }
     
    // Debug spew -- decrements on exit
    (*(pParam->puCount))--;
    printf( _T("OUT -- Thread Count: %d\n"), *(pParam->puCount) );
     
    // Signals parent waiting that this thread has exited
    SetEvent( pParam->hEvent );
     
    return 0;
    }
     
    int _tmain(int argc, _TCHAR* argv[])
    {
    UINT uCount = 0;
    ThreadParam tp = { 2 * 3.14,
    	0,
    	0.01, // 0.015
    	::CreateEvent( NULL, FALSE, FALSE, NULL ),
    	0.0,
    	&uCount };
     
    // Queue the first work item using the Windows thread pooling mechanism
    ::QueueUserWorkItem( Integral, &tp, WT_EXECUTELONGFUNCTION );
     
    // Wait for the event to signal
    WaitForSingleObject( tp.hEvent, INFINITE); 
     
    // Cleanup the event handle
    ::CloseHandle( tp.hEvent );
     
    printf(" %f ", tp.dResultToSend);
    printf("press any key");
     
    getch();
     
    return 0;
    }

  12. #12
    Join Date
    Apr 2005
    Posts
    75

    Re: What is the maximum number of threads per process in Windows

    thanks but i keep getting errors at

    error C2039: 'QueueUserWorkItem' : is not a member of '`global namespace''
    error C2065: 'QueueUserWorkItem' : undeclared identifier
    error C2065: 'WT_EXECUTELONGFUNCTION' : undeclared identifier

  13. #13
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    Re: What is the maximum number of threads per process in Windows

    Quote Originally Posted by raindog
    thanks but i keep getting errors at

    error C2039: 'QueueUserWorkItem' : is not a member of '`global namespace''
    error C2065: 'QueueUserWorkItem' : undeclared identifier
    error C2065: 'WT_EXECUTELONGFUNCTION' : undeclared identifier
    Did you add the following to the top of your #includes?

    Code:
    #ifndef _WIN32_WINNT
    #define _WIN32_WINNT 0x0500
    #endif


    See the attachment for the complete solution.

    Arjay
    Attached Files Attached Files
    Last edited by Arjay; May 3rd, 2005 at 03:06 AM.

  14. #14
    Join Date
    Apr 2005
    Posts
    75

    Re: What is the maximum number of threads per process in Windows

    Quote Originally Posted by Arjay
    Did you add the following to the top of your #includes?
    See the attachment for the complete solution.
    Arjay
    i did and still won't work ... i use visual studio 6 c++ and i have to create it in that application... what did you use to create the project you sent me in the zip?

  15. #15
    Join Date
    Apr 2005
    Posts
    75

    Re: What is the maximum number of threads per process in Windows

    i'm a bit new to all this what does ::CloseHandle() instead of CloseHandle() mean?
    and why did you write _tmain instead of main

Page 1 of 3 123 LastLast

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