Limiting number of simultaneous threads
Is there any technique to limit the number of simultaneous threads?
I wanna execute only 5 threads at a time. I dont wanna stop the main process while waiting for the threads to complete..
[197 byte] By [
Smooth1] at [2007-11-20 0:50:28]

# 1 Re: Limiting number of simultaneous threads
Is there any technique to limit the number of simultaneous threads?The simple solution is to limit the number of threads created.
If you want to allow an 'unlimited' numbers of threads, but only 5 threads active at the same time (the others one in a waiting state) then you can use a completion ports. Take a look at CreateIOCompletionPort (http://msdn.microsoft.com/library/default.asp?url=/library/en-us/fileio/fs/createiocompletionport.asp).
- petter
# 2 Re: Limiting number of simultaneous threads
You can use a counter variable probably a static one. Let the main thread create another thread only if the counter is below 5. Decrement the counter if a thread finishes its job.
Take a look at the following:
WaitForMultipleObjects (http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dllproc/base/waitformultipleobjects.asp)
Also you can do a google search to see how thread pools are implemented.
# 3 Re: Limiting number of simultaneous threads
if i use the counter method, the main process gets blocked as it has to wait for the thread count to reach 5, hence the application would freeze (it doesnt paint).
# 4 Re: Limiting number of simultaneous threads
[ Redirected thread ]
# 5 Re: Limiting number of simultaneous threads
if i use the counter method, the main process gets blocked as it has to wait for the thread count to reach 5, hence the application would freeze (it doesnt paint).Why does it have to wait? Maybe you should share some of your application details? MAybe you could post some code?
- petter
# 6 Re: Limiting number of simultaneous threads
ok, here's the structure
UINT myclass::processDataofDay(LPVOID param) //thread procedure
{
}
void myclass::onClick()
{
for(int day=1;day<200;day++)
{
//creating parameter object
AfxBeginThread(processDataofDay, param);
}
}
# 7 Re: Limiting number of simultaneous threads
So, due to the time it takes to create 200 threads your main thread seems to freeze? And you want to avoid that?
Well, one solution is to create one extra thread, that in turn creates the 200 thread you need, this way your main thread can continue on to the message pump (or whatever it needs to do).
Anyway, there is always some overhead with threads (especially when switching among several threads), so you might experience a performance loss when creating (and using) that many.
- petter
# 8 Re: Limiting number of simultaneous threads
Actually, the CPU becomes 100%, and the system becomes slow.. So i wanna run only 5 threads at a time and once the No of running threads is <5, the next thread should be started..
but if i use something like
onClick()
{
while(count>=5) // to stop creating new thread if running thread count is > 5
{
}
AfxBeginthread();
}
This way, the main thread gets blocked.. i dont want more than 5 threads running at a time.. this simply freezes the system.
# 9 Re: Limiting number of simultaneous threads
Actually, the CPU becomes 100%, and the system becomes slow.. So i wanna run only 5 threads at a time and once the No of running threads is <5, the next thread should be started..If the thread do doing 'heavy number crunching' you're probably better off with only 1 thread per cpu/core.
Anyway, I suggest that you do as miteshpandey said, and look up 'thread pools'.
Another option is to create your 5 threads (or whatever), and when the thread is about to terminate (just before the thread procedure exits) you can send a notification back to the main window/thread. For that you can use SendNotifyMessage or PostMessage. Then handle this message in the main windows message pump and start up a new thread.
- petter