473,503 Members | 3,740 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Controlling Threads With MUTEX

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
Nov 21 '05 #1
2 1745
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

"Alex" wrote:
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

Nov 21 '05 #2
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

"Alex" wrote:
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

Nov 21 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
2291
by: Andreas Müller | last post by:
i need two loops that run forever. one of it receives data and stores it to a vector. the other one writes the elements of the vector to the disk. this means the vector is a receiver buffer. How...
10
7442
by: Alfonso Morra | last post by:
This may be considered as OT since the C++ Standard says not one word about threads. Nevertheless, C++ is routinely and widely used to write solid multithreaded code. I wondered if anyone has...
1
2947
by: Bryan Ax | last post by:
We have some code that's having problems when multiple applications running as different identities try to hit this code - the code creates a Mutex, and when the second identity tries to access it,...
0
270
by: Alex | last post by:
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....
1
928
by: Alex | last post by:
Dear colleagues I have a simple function which calls through a function some threads. The function looks like follow: Private Sub Refresh( ) Call_ManyOtherThreads_Function(ToDo ) '--> This...
10
8563
by: Mark Denardo | last post by:
Hi, I have multi-threaded application that requires one particular thread (Non-Form based) to receive data from other threads. I know how to pass control to Form Threads, but I believe there's no...
8
4387
by: JackC | last post by:
Hi, I am trying to get posix threads working from within an object, heres some code: int NConnection::TheadControl() { int thread_id; pthread_t new_connection;...
1
1614
by: gallois | last post by:
hello everyone, i've been trying to write something that checks the inputs from the possible devices where a mouse can be connected (debian/linux) in order to make it configure the system...
167
8152
by: darren | last post by:
Hi I have to write a multi-threaded program. I decided to take an OO approach to it. I had the idea to wrap up all of the thread functions in a mix-in class called Threadable. Then when an...
0
7064
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7261
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7315
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
7445
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
1
4991
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4665
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3147
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1492
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
369
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.