473,799 Members | 3,329 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Waiting on multiple Threads

Hi all,

What's the best way to queue up and wait for number of threads to complete.
This problem was trivial in VC++ 6 but I'm finding it rather hard to solve
in VB.NET. My calculations run about 2 mins each (on a 2.8 Ghz Xeon and the
server range from 2 to 8 processors) (give or take about 15 secs). I have 8
sets of calculations to do (in another app I have 121) all the same calc
just different data. But the rest of the processing cannot continue until
ALL of the calculations are done.

so here's the algorithm I want to use (written from the C++ implentation
standpoint) {for ease of reading i have left out any/all discussion about
the queue ops used to schedule the runs which can change to what ever is
appropriate}
------------------------------------------------------------------
get user pref on number of concurrent threads...
for loop (to number of concurrent threads)
start a thread (with data)
add to wait list
while true
WaitForMultiple Objects(...thre ads...)
if more threads to process
launch thread with data
add to wait list
else if all threads finished
break
------------------------------------------------------------------

The problem I'm having is finding the .NET equivalent to
WaitForMultiple Objects. The threads might not complete in order or might
complete at the same time. I was toying with the idea of a delegate that
would basically have the functionality of the while loop, but seemed like
spaghetti coding to me.

Any ideas or examples you can think of would be greatly appreciated.

Thanks in advance,
Mike



----------------------------------------------------------
Mike Elledge
Jul 21 '05 #1
10 3071
<"MikeE" <mikeDOTelledge ATattbiDOTcom>> wrote:
What's the best way to queue up and wait for number of threads to complete.
This problem was trivial in VC++ 6 but I'm finding it rather hard to solve
in VB.NET. My calculations run about 2 mins each (on a 2.8 Ghz Xeon and the
server range from 2 to 8 processors) (give or take about 15 secs). I have 8
sets of calculations to do (in another app I have 121) all the same calc
just different data. But the rest of the processing cannot continue until
ALL of the calculations are done.


That's easy then - just call Join() on all the threads.

--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Jul 21 '05 #2
Jon,

Doesn't Join() block until that particular thread is done (the same as
WaitForSingleOb ject)? If so, then that won't give me the proper scheduling
right? When any of the current threads end, I need to schedule the next in
the queue, and I can't schedule (i.e., start()) the next one UNTIL one of
the MAX # finishes.

Please correct me if i'm wrong about Join().

Thanks,
ME

"Jon Skeet [C# MVP]" <sk***@pobox.co m> wrote in message
news:MP******** *************** *@msnews.micros oft.com...
<"MikeE" <mikeDOTelledge ATattbiDOTcom>> wrote:
What's the best way to queue up and wait for number of threads to
complete.
This problem was trivial in VC++ 6 but I'm finding it rather hard to
solve
in VB.NET. My calculations run about 2 mins each (on a 2.8 Ghz Xeon and
the
server range from 2 to 8 processors) (give or take about 15 secs). I have
8
sets of calculations to do (in another app I have 121) all the same
calc
just different data. But the rest of the processing cannot continue
until
ALL of the calculations are done.


That's easy then - just call Join() on all the threads.

--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too

Jul 21 '05 #3
<"MikeE" <mikeDOTelledge ATattbiDOTcom>> wrote:
Doesn't Join() block until that particular thread is done (the same as
WaitForSingleOb ject)?
Yes.
If so, then that won't give me the proper scheduling right?
Nope.
When any of the current threads end, I need to schedule the next in
the queue, and I can't schedule (i.e., start()) the next one UNTIL one of
the MAX # finishes.

Please correct me if i'm wrong about Join().


Maybe I've misunderstood you. I thought you had created a bunch of
threads, started them running, and then wanted to know when they'd all
finished. In that case, calling Join() on each of them in turn does
exactly what you want.

If that's not what you're doing, you'll need to give a bit more
information about what you're doing.

--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Jul 21 '05 #4
Jon,

I'll try to restate then...

I have X number of tasks (unknown run time) that must be completed before
additional work is done. The USER specifies that the system can use N
threads to do the processing in parallel. How do we get this done in
VB.NET? The VC++ implentation is as follows...

1) Queue all work data (X items)
2) For i = 0; i < N; i++
2a) Get work data from queue (i.e., remove one item)
2b) Create Thread with work Data
2c) Start Thread
2d) Add Thread to watch list
3) while (true)
3a) WaitForMultiple Objects(watch list) <--- this will
block until ANY of the threads return
3b) remove finished thread from watch list
3c) If work data queue NOT empty
3c1) Get work data from queue (i.e., remove one item)
3c2) Create Thread with work Data
3c3) Start Thread
3c4) Add Thread to watch list
3d) else if all threads done (watch list empty)
3d1) break; <--
break the while loop when ALL data is processed and ALL threads are done
// else still processing threads...
4) additional work...
So another way to state my question is...
How do I block on multiple threads in VB.NET? Are WaitHandles my only
option?

Thanks,

"Jon Skeet [C# MVP]" <sk***@pobox.co m> wrote in message
news:MP******** *************** *@msnews.micros oft.com...
<"MikeE" <mikeDOTelledge ATattbiDOTcom>> wrote:
Doesn't Join() block until that particular thread is done (the same as
WaitForSingleOb ject)?


Yes.
If so, then that won't give me the proper scheduling right?


Nope.
When any of the current threads end, I need to schedule the next in
the queue, and I can't schedule (i.e., start()) the next one UNTIL one
of
the MAX # finishes.

Please correct me if i'm wrong about Join().


Maybe I've misunderstood you. I thought you had created a bunch of
threads, started them running, and then wanted to know when they'd all
finished. In that case, calling Join() on each of them in turn does
exactly what you want.

If that's not what you're doing, you'll need to give a bit more
information about what you're doing.

--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too

Jul 21 '05 #5
Mike,
It sounds like you are wanting to write your own thread pool.

Any reason you are not using System.Threadin g.ThreadPool itself?
If ThreadPool was not quite what I needed, instead of creating a distinct
thread for each request I would simply create N threads. I would place all
the requests into a single System.Collecti ons.Queue, each thread would read
a request from the queue, process it, then get another request from the
queue. Of course the Queue itself would need to be wrapped in thread safe
code such as via Queue.Synchroni zed. You may need to implement something to
let each thread know when it should stop processing requests...

Hope this helps
Jay

"MikeE" <mikeDOTelledge ATattbiDOTcom> wrote in message
news:%2******** ********@TK2MSF TNGP12.phx.gbl. ..
Jon,

I'll try to restate then...

I have X number of tasks (unknown run time) that must be completed before
additional work is done. The USER specifies that the system can use N
threads to do the processing in parallel. How do we get this done in
VB.NET? The VC++ implentation is as follows...

1) Queue all work data (X items)
2) For i = 0; i < N; i++
2a) Get work data from queue (i.e., remove one item)
2b) Create Thread with work Data
2c) Start Thread
2d) Add Thread to watch list
3) while (true)
3a) WaitForMultiple Objects(watch list) <--- this will
block until ANY of the threads return
3b) remove finished thread from watch list
3c) If work data queue NOT empty
3c1) Get work data from queue (i.e., remove one item)
3c2) Create Thread with work Data
3c3) Start Thread
3c4) Add Thread to watch list
3d) else if all threads done (watch list empty)
3d1) break; <--
break the while loop when ALL data is processed and ALL threads are done
// else still processing threads...
4) additional work...
So another way to state my question is...
How do I block on multiple threads in VB.NET? Are WaitHandles my only
option?

Thanks,

"Jon Skeet [C# MVP]" <sk***@pobox.co m> wrote in message
news:MP******** *************** *@msnews.micros oft.com...
<"MikeE" <mikeDOTelledge ATattbiDOTcom>> wrote:
Doesn't Join() block until that particular thread is done (the same as
WaitForSingleOb ject)?


Yes.
If so, then that won't give me the proper scheduling right?


Nope.
When any of the current threads end, I need to schedule the next in
the queue, and I can't schedule (i.e., start()) the next one UNTIL one
of
the MAX # finishes.

Please correct me if i'm wrong about Join().


Maybe I've misunderstood you. I thought you had created a bunch of
threads, started them running, and then wanted to know when they'd all
finished. In that case, calling Join() on each of them in turn does
exactly what you want.

If that's not what you're doing, you'll need to give a bit more
information about what you're doing.

--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too


Jul 21 '05 #6
<"MikeE" <mikeDOTelledge ATattbiDOTcom>> wrote:
I'll try to restate then...

I have X number of tasks (unknown run time) that must be completed before
additional work is done. The USER specifies that the system can use N
threads to do the processing in parallel. How do we get this done in
VB.NET? The VC++ implentation is as follows...


That sounds exactly like a thread pool. If the system thread pool
doesn't work for you (and there are various reasons why you might not
want to use it) you may want to look at the implementation I've got at
http://www.pobox.com/~skeet/csharp/miscutil

It's not been tested much, but you're welcome to it...

--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Jul 21 '05 #7
John/Jay,
Yes! what I want is so close to the threadpool that it is irritating. The
ONE MAJOR missing part for a thread pool is the MAX number of threads. See,
this processing is some rather lengthy numerical calculations, VERY CPU
intensive. We do not run it on any machine that does not have at least 2
processors (or one hyper threaded one, and then just for testing).
Otherwise it locks the system up. But we have some servers that have up to
8 CPUs in them. So we need the number of concurrent threads to be variable.
If we just lanched them all at once, we would QUICKLY run out of cpu
bandwith for the calcs and the system. (back to locked systems). we just
have to be able to control the number of "active" threads at one time.

What would your thoughts/feelings be towards using "WaitForMultipl eObjects".
We of course could import it (which ive done) but having trouble setting up
the array of Thread handles to be watched. Any ideas? (just a reminder...i
kinda need this in vb.net)

On another note.. since i do have to get something working soon.. i've
started down the spaghetti code method, where the threads fire events to
which i have a re-enterable function that can access the remaining queue,
and when finally empty and done fires another event that is handled by a
delegate that does "the rest of the stuff" Unfortunatly this is looking
REALLY ugly, so i'm trying to come up with a class to wrap the thread and
that contains a wait handle which is montitored.. but still trying to flush
this out.

Again, what gets me, is that this takes less than 50 lines of code in VC++,
with error checking, and was rock solid. I'm really having a tough time
coming to grips with the fact that we can't do it EVEN EASIER in .NET like
so many of our other upgrades.

And john, thanks for the link.. i'll take a look at miscutil tomorrow!

Thanks again for your time on this,
ME

"Jon Skeet [C# MVP]" <sk***@pobox.co m> wrote in message
news:MP******** *************** *@msnews.micros oft.com...
<"MikeE" <mikeDOTelledge ATattbiDOTcom>> wrote:
I'll try to restate then...

I have X number of tasks (unknown run time) that must be completed before
additional work is done. The USER specifies that the system can use N
threads to do the processing in parallel. How do we get this done in
VB.NET? The VC++ implentation is as follows...


That sounds exactly like a thread pool. If the system thread pool
doesn't work for you (and there are various reasons why you might not
want to use it) you may want to look at the implementation I've got at
http://www.pobox.com/~skeet/csharp/miscutil

It's not been tested much, but you're welcome to it...

--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too

Jul 21 '05 #8
<"Mike E" <mikeDOTelledge ATattbiDOTcom>> wrote:
Yes! what I want is so close to the threadpool that it is irritating. The
ONE MAJOR missing part for a thread pool is the MAX number of threads. See,
this processing is some rather lengthy numerical calculations, VERY CPU
intensive. We do not run it on any machine that does not have at least 2
processors (or one hyper threaded one, and then just for testing).
Otherwise it locks the system up. But we have some servers that have up to
8 CPUs in them. So we need the number of concurrent threads to be variable.
If we just lanched them all at once, we would QUICKLY run out of cpu
bandwith for the calcs and the system. (back to locked systems). we just
have to be able to control the number of "active" threads at one time.

What would your thoughts/feelings be towards using "WaitForMultipl eObjects".
We of course could import it (which ive done) but having trouble setting up
the array of Thread handles to be watched. Any ideas? (just a reminder...i
kinda need this in vb.net)
Well, you don't really want to wait for other threads to actually
finish, because you can't reuse them. Doing a thread pool of *some*
description (which could just be several threads with a
producer/consumer queue - see
http://www.pobox.com/~skeet/csharp/t...eadlocks.shtml for some
sample code) is a much better answer here.

Alternatively, using Auto/ManualResetEven ts, one per thread, would
allow you to wait for any of the threads to finish without being too
hard. Or just have a single monitor which the main thread waits on and
which other threads pulse.
On another note.. since i do have to get something working soon.. i've
started down the spaghetti code method, where the threads fire events to
which i have a re-enterable function that can access the remaining queue,
and when finally empty and done fires another event that is handled by a
delegate that does "the rest of the stuff" Unfortunatly this is looking
REALLY ugly, so i'm trying to come up with a class to wrap the thread and
that contains a wait handle which is montitored.. but still trying to flush
this out.

Again, what gets me, is that this takes less than 50 lines of code in VC++,
with error checking, and was rock solid. I'm really having a tough time
coming to grips with the fact that we can't do it EVEN EASIER in .NET like
so many of our other upgrades.


You may well find it's easier in .NET, once you change the approach
slightly. There are lots of things which are hard to do if you try
using the "old" approach, but for which there are new approaches which
make things easier.

--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Jul 21 '05 #9
Jon,

Thanks, I'll look into the single monitor option... seems promising.

One clarification though.. I HAVE to wait for all the threads to finish, not
because I want to reuse the thread, but because I need the "ANSWER". A math
problem is not finished until all the parts are there. In our program the
final result can NOT be calculated until ALL the threads have returned.

Thanks again,
ME

"Jon Skeet [C# MVP]" <sk***@pobox.co m> wrote in message
news:MP******** *************** *@msnews.micros oft.com...
<"Mike E" <mikeDOTelledge ATattbiDOTcom>> wrote:
Yes! what I want is so close to the threadpool that it is irritating.
The
ONE MAJOR missing part for a thread pool is the MAX number of threads.
See,
this processing is some rather lengthy numerical calculations, VERY CPU
intensive. We do not run it on any machine that does not have at least 2
processors (or one hyper threaded one, and then just for testing).
Otherwise it locks the system up. But we have some servers that have up
to
8 CPUs in them. So we need the number of concurrent threads to be
variable.
If we just lanched them all at once, we would QUICKLY run out of cpu
bandwith for the calcs and the system. (back to locked systems). we
just
have to be able to control the number of "active" threads at one time.

What would your thoughts/feelings be towards using
"WaitForMultipl eObjects".
We of course could import it (which ive done) but having trouble setting
up
the array of Thread handles to be watched. Any ideas? (just a
reminder...i
kinda need this in vb.net)


Well, you don't really want to wait for other threads to actually
finish, because you can't reuse them. Doing a thread pool of *some*
description (which could just be several threads with a
producer/consumer queue - see
http://www.pobox.com/~skeet/csharp/t...eadlocks.shtml for some
sample code) is a much better answer here.

Alternatively, using Auto/ManualResetEven ts, one per thread, would
allow you to wait for any of the threads to finish without being too
hard. Or just have a single monitor which the main thread waits on and
which other threads pulse.
On another note.. since i do have to get something working soon.. i've
started down the spaghetti code method, where the threads fire events to
which i have a re-enterable function that can access the remaining queue,
and when finally empty and done fires another event that is handled by a
delegate that does "the rest of the stuff" Unfortunatly this is looking
REALLY ugly, so i'm trying to come up with a class to wrap the thread and
that contains a wait handle which is montitored.. but still trying to
flush
this out.

Again, what gets me, is that this takes less than 50 lines of code in
VC++,
with error checking, and was rock solid. I'm really having a tough time
coming to grips with the fact that we can't do it EVEN EASIER in .NET
like
so many of our other upgrades.


You may well find it's easier in .NET, once you change the approach
slightly. There are lots of things which are hard to do if you try
using the "old" approach, but for which there are new approaches which
make things easier.

--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too

Jul 21 '05 #10

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

Similar topics

5
3851
by: bughunter | last post by:
Hi, Consider this code: ---- Monitor.Pulse(oLock); Monitor.Exit(oLock); ---- If a thread was waiting on oLock then will the current thread
3
12331
by: Douwe | last post by:
I'm trying to write a piece of C code that has two threads: The first thread waits for a socket connection. The second thread is in a continues cycle waiting for an period of time (lets say 500 ms) or an incomming socket connection (received from the first thread). At this moment I've created two threads were the first one is already waiting for an incomming socket connection. I've also created a timer with timer_create. My problem is that...
3
3317
by: mphanke | last post by:
Hi, I would like to implement a windows service which forks a configurable number of threads which all siten to the same port and reply to requests of different clients. How would I implement something like this? Martin
3
12602
by: Elhurzen | last post by:
X-No-Archive: Yes >From what I understand, if multiple threads are waiting on a ManualResetEvent after calling WaitOne(), only one thread is guaranteed to be signaled when Set() is called on that ManualResetEvent. How do I ensure that _all_ waiting threads are signaled, without sacrificing any of the functionality or much of the simplicity of ManualResetEvent? Example: SignalingClass:
2
2104
by: Tumurbaatar S. | last post by:
ASP.NET QuickStart Tutorial says that: .... ASP.NET maintains a pool of HttpApplication instances over the course of a Web application's lifetime. ASP.NET automatically assigns one of these instances to process each incoming HTTP request that is received by the application. The particular HttpApplication instance assigned is responsible for managing the entire lifetime of the request and is reused only after the request has been...
10
590
by: MikeE | last post by:
Hi all, What's the best way to queue up and wait for number of threads to complete. This problem was trivial in VC++ 6 but I'm finding it rather hard to solve in VB.NET. My calculations run about 2 mins each (on a 2.8 Ghz Xeon and the server range from 2 to 8 processors) (give or take about 15 secs). I have 8 sets of calculations to do (in another app I have 121) all the same calc just different data. But the rest of the processing...
16
1962
by: Bruce Wood | last post by:
Maybe it's just late in my day, but I'm reading Jon's article on threading, in particular how to use Monitor.Wait() and Monitor.Pulse(), and there's something that's not sinking in. The code in question looks like this: public class ProducerConsumer { readonly object listLock = new object(); Queue queue = new Queue();
4
6176
by: jankhana | last post by:
Hi all, I'm having an application in that i use Sql Compact 3.5 with VS2008. I'm running multiple threads in my application which contacts the compact database and accesses the row. It selects and deletes those rows in a fashion i.e selecting and giving to the application 5 rows and deleting those rows from the table. It works great with a single thread but if i use multiple threads i.e if 3 or more threads are running I get very...
0
9541
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10252
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10231
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9073
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7565
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5585
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4141
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 we have to send another system
2
3759
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2938
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 can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.