Re: What is the maximum number of threads per process in Windows
i've solved the problem using my old design by inserting a delay in creation of the treads this way i get that some calculating threas finish the work before the rest of the tree is created thus freeing up the used treads count the only chage i made is
Code:
ThreadParam* pParam1 = new ThreadParam; //parametri prve niti
pParam1->lLower = pParam->lLower;
pParam1->lUpper = pParam->lLower + (lSizeOfMyInterval)/2;
pParam1->lDeltaX = pParam->lDeltaX;
ThreadHandles[0] = CreateThread(NULL, 0, Integral, (LPVOID)pParam1, 0, 0);
Sleep(100); //HERE
ThreadParam* pParam2 = new ThreadParam; //parametri druge niti
pParam2->lLower = pParam->lLower + (lSizeOfMyInterval)/2;
pParam2->lUpper = pParam->lUpper;
pParam2->lDeltaX = pParam->lDeltaX;
ThreadHandles[1] = CreateThread(NULL, 0, Integral, (LPVOID)pParam2, 0, 0);
Re: What is the maximum number of threads per process in Windows
Quote:
Originally Posted by raindog
what operating sistem do u use ... i use XP SP2 ... if you use some other maybe that is why it works with you ... and you see an average of 400 threads because the rest of the screen flies by and you only see the exiting stage but to reach the lowest children you still need to create more than a 1000 threads when using 0.01 .... just a thought :)
I'm running on Win2K. Both your program and mine work. The programs only create a maximum of 400 - 550 threads. How do I know this? Because I change the buffer size in the console window and capture the entire output - see the attached file. So we are well under 1000 threads.
Re: What is the maximum number of threads per process in Windows
Quote:
Originally Posted by Arjay
I'm running on Win2K. Both your program and mine work. The programs only create a maximum of 400 - 550 threads. How do I know this? Because I change the buffer size in the console window and capture the entire output - see the attached file. So we are well under 1000 threads.
then it must be to XP because the file i've sent you doesn't work on my comp it hangs on 500 threads
Re: What is the maximum number of threads per process in Windows
Quote:
Originally Posted by raindog
i've solved the problem using my old design by inserting a delay in creation of the treads this way i get that some calculating threas finish the work before the rest of the tree is created thus freeing up the used treads count the only chage i made is
Code:
ThreadParam* pParam1 = new ThreadParam; //parametri prve niti
pParam1->lLower = pParam->lLower;
pParam1->lUpper = pParam->lLower + (lSizeOfMyInterval)/2;
pParam1->lDeltaX = pParam->lDeltaX;
ThreadHandles[0] = CreateThread(NULL, 0, Integral, (LPVOID)pParam1, 0, 0);
Sleep(100); //HERE
ThreadParam* pParam2 = new ThreadParam; //parametri druge niti
pParam2->lLower = pParam->lLower + (lSizeOfMyInterval)/2;
pParam2->lUpper = pParam->lUpper;
pParam2->lDeltaX = pParam->lDeltaX;
ThreadHandles[1] = CreateThread(NULL, 0, Integral, (LPVOID)pParam2, 0, 0);
Really the fundamental problem is a design that uses so many threads. You can raise the max thread count (have you looked into WT_SET_MAX_THREADPOOL_THREAD?), but the underlying problem is that this design requires too many threads. I understand that this is an classroom exercise, and that you need to do it this way, but this approach wouldn't be of benefit in the real world. I say this because, unless you are on a multiproc (and I mean like 16+ processors or more), you are going to be burning cycles with context switches and thus wasting time. Btw, the sleep that you have added above may work on your machine but it isn't really a true fix because on another machine that sleep value probably won't get the job done. In addition, if you were to use a sleep, then you may consider to use it with the QueueUserWorkItem approach - because with the QUWI approach, you at least have the possibility of using less threads than total nodes.
Arjay
Re: What is the maximum number of threads per process in Windows
Quote:
Originally Posted by raindog
then it must be to XP because the file i've sent you doesn't work on my comp it hangs on 500 threads
XP probably has a reason for limiting the thread count, can you figure out what that might be?
Arjay
Re: What is the maximum number of threads per process in Windows
Each Thread has a default Stack size of 1 MB. If you create a thread 1 MB is reserved in the address space of your process. It is not commited yet (so your stack does not really allocates 1 MB of physical Ram until the stack is needed). In Windows you have a 2 GB address space limit. This allows you to create ca. 2000 Threads with the default Stack size in your process. There is no real limit as far as I know but you will soon hit the physical address space problem.
You can go to 3 GB with a special swith during startup of windows but this problem will be solved in XP64 once and for all.
Why? Because the address space is much bigger than the storage capacity of the human brain which is approximately 1 PB (1 Peta Byte 10^15) But you have over 2^64 = 10^19 Bytes which means you would have 10 000 more storage capacity than the human brain. This should be enough for everyone ;-)
Re: What is the maximum number of threads per process in Windows
i gave it a few days rest and finaly came up with an idea this way the thread count will be equal to the depth of the binary tree :) and you can make the deltaX whatever small you want
Code:
#include <windows.h>
#include <stdio.h>
#include <cassert>
#include <conio.h>
#include <math.h>
struct ThreadParam {
double dUpper;
double dLower;
double dDeltaX;
double dResultToSend; // Returns the result
UINT* puCount; // Debug Only - tracks thread count
};
CRITICAL_SECTION FunctionAccess;
double CalculateFunction(double dPoint, double dSizeOfInterval){
return exp(-0.5 * dPoint)
* cos(dPoint)
* dSizeOfInterval;
}
DWORD WINAPI Integral(LPVOID param)
{
static double dSizeOfMyInterval = 0.0;
ThreadParam* pParam = (ThreadParam*) param;
// increments thread count when enters fn; decrements on exit
(*(pParam->puCount))++;
printf("IN -- Thread Count: %d\n", *(pParam->puCount) );
dSizeOfMyInterval = pParam->dUpper - pParam->dLower;
if(dSizeOfMyInterval > pParam->dDeltaX)
{
ThreadParam* pParam1 = new ThreadParam;
pParam1->dLower = pParam->dLower;
pParam1->dUpper = pParam->dLower + (dSizeOfMyInterval) / 2;
pParam1->dDeltaX = pParam->dDeltaX;
pParam1->dResultToSend = 0.0;
pParam1->puCount = pParam->puCount;
ThreadParam* pParam2 = new ThreadParam;
pParam2->dLower = pParam->dLower + (dSizeOfMyInterval) / 2;
pParam2->dUpper = pParam->dUpper;
pParam2->dDeltaX = pParam->dDeltaX;
pParam2->dResultToSend = 0.0;
pParam2->puCount = pParam->puCount;
HANDLE* ThreadHandles = new HANDLE[2];
ThreadHandles[0]=CreateThread(NULL, 0, Integral, (LPVOID)pParam1, 0, 0);
if (ThreadHandles[0]==NULL){
(*(pParam->puCount))--;
printf("OUT IR-- Thread Count: %d\n", *(pParam->puCount) );
return 0;
}
//Sleep(5);
ThreadHandles[1]=CreateThread(NULL, 0, Integral, (LPVOID)pParam2, CREATE_SUSPENDED, 0);
if (ThreadHandles[1]==NULL){
(*(pParam->puCount))--;
printf("OUT IR -- Thread Count: %d\n", *(pParam->puCount) );
return 0;
}
WaitForSingleObject(ThreadHandles[0],INFINITE);
ResumeThread(ThreadHandles[1]);
WaitForSingleObject(ThreadHandles[1],INFINITE);
//WaitForMultipleObjects(2,ThreadHandles,TRUE,INFINITE);
CloseHandle(ThreadHandles[0]);
CloseHandle(ThreadHandles[1]);
pParam->dResultToSend = pParam1->dResultToSend + pParam2->dResultToSend; //sending result to parent
}
else
{
//calcualting the function in the specific point
EnterCriticalSection(&FunctionAccess);
pParam->dResultToSend=CalculateFunction(pParam->dLower+dSizeOfMyInterval/2,dSizeOfMyInterval);
LeaveCriticalSection(&FunctionAccess);
}
// decrements on exit
(*(pParam->puCount))--;
printf("OUT -- Thread Count: %d\n", *(pParam->puCount) );
return 0;
}
int main(int argc, CHAR* argv[])
{
DWORD dStartTime;
DWORD dEndTime;
HANDLE* ThreadHandles = new HANDLE[1];
UINT uCount = 0;
ThreadParam* pParam = new ThreadParam;
pParam->dLower = 0;
pParam->dUpper = 2 * 3.141592654;
pParam->dDeltaX = 0.00001;
pParam->dResultToSend = 0.0;
pParam->puCount = &uCount;
dStartTime=GetTickCount();
InitializeCriticalSection(&FunctionAccess);
ThreadHandles[0]=CreateThread(NULL, 0, Integral, (LPVOID)pParam, 0, 0);
WaitForSingleObject( ThreadHandles[0], INFINITE);
CloseHandle( ThreadHandles[0]);
DeleteCriticalSection(&FunctionAccess);
printf(" %f ", pParam->dResultToSend);
dEndTime=GetTickCount();
printf("\nElapsed time in miliseconds %d ",dEndTime-dStartTime);
printf("\npress any key");
getch();
return 0;
}