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
10 3073
MikeE,
Why not use two queues?
One queue for the questions (requests) to the N threads in the pool and a
second queue for the answers (to the main thread). The main thread can
simply wait for answers on the second queue, once it has all the answers it
can figure the final result.
Hope this helps
Jay
"MikeE" <mikeDOTelledge ATattbiDOTcom> wrote in message
news:e4******** ******@TK2MSFTN GP15.phx.gbl... 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
This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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
|
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...
|
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
|
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:
|
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...
| |
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...
|
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();
|
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...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
|
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,...
|
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...
| |
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,...
|
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...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
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
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |