Controlling Threads With MUTEX

  • Thread starter Thread starter Alex
  • Start date Start date
A

Alex

Dear colleagues

I am programming an application which starts a lot of threads for some
calculations. The problem that I have is that when I start up to 100 threads
then everything works fine. Depending of number of jobs it might be that
sometimes application need to start up to 4000 threads for this small jobs.
You see probably already the problem. This is not good idea to start 5000
threads at the same time (memory).

My idea is to have a function which controls the current number of working
threads. I would like to have something like 100 threads in the memory at
one time and the rest must be waiting or might not be even initiated. Once a
thread is finished the function should take one waiting job and start it
with a new thread.

Is it possible for example to control this with Mutex. e.g. to define the
muttex to 100. Up 100 the mutex let's the procedure waiting. Once a mutex is
released and somehow internally the mutex has value of 99 the new muttex may
increase the muttex to 100. The next one can't increase because the muttex
is limited to 100. So he waits until muttex has valu under 100 end so on.

If this is not possible, do you have other ideas?

THANKS ALEX SIMIC
 
Hi Alex,

You could use the ThreadPool, which has a default limit of 25 threads, if
your calculations are relatively short lived. The ThreadPool will queue any
requests that come in while all the threads are busy. This number can be
amended, and here is a link I think shows how:
http://staff.develop.com/woodring/dotnet/#tpcontrol.

A mutex is really a synchronisation object, that allows you to control
access to shared resources across multiple processes. Context switching (and
so performance) will probably be a bigger concern than memory if you have a
very large number of threads operating.

If the built in ThreadPool is not sufficient, just search Google for custom
thread pools, as there are lots of samples out there.

Hope this helps
Dan
 
Back
Top