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.
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
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(...).
Re: What is the maximum number of threads per process in Windows
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()
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).
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.
Re: What is the maximum number of threads per process in Windows
I took the liberty of changing the code slightly:
Rather than using the thread return param to pass the result, the code now passes the result in the ThreadParam structure
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.
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.
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;
}
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
Re: What is the maximum number of threads per process in Windows
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?
Re: What is the maximum number of threads per process in Windows
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?
* The Best Reasons to Target Windows 8
Learn some of the best reasons why you should seriously consider bringing your Android mobile development expertise to bear on the Windows 8 platform.