473,397 Members | 1,969 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,397 software developers and data experts.

Threading advice: How to wait for all threads to complete withoutpolling.

Greetings all,

It is my understanding that polling is very bad (I could be wrong,
this is just my understanding). But I have a problem that I don't know
how to solve without polling.

I need to execute a series of mathematically intensive methods and
wait for them ALL to finish before moving on to the next step. I have
a machine with lots of cores and lots of memory so threading seems
like a great solution. To be exact the BackgroundWorker seems to be a
good solution because I would like to limit how many of these problems
are solved concurrently (to limit the hit on resources). Here is my
solution
MyMethod()
{
List<BackgroundWorkermyWorkers new List<BackgroundWorker>();
Add problems 1 through n to be solved to list as new
BackgroundWorkers
Limit the number of concurrent threads firing on the ThreadPool

foreach(BackgroundWorker bw in myWorkers)
{
bw.RunWorkerAsync();
}

while(myWorkers.Count 0)
{
Sleep a little while (say 100)
Check if each of myWorkers are busy, if not, mark them for removal
Remove finished workers from myWorkers
}

Go on to next task that requires all of the above workers to be
finished first
}

But as I said, it is my understanding that this sort of polling is
very bad. Is this assumption correct? If so, what is the better way?

Randy
Jun 27 '08 #1
3 2863
On Jun 18, 12:23*pm, keeling <randykeel...@gmail.comwrote:
But as I said, it is my understanding that this sort of polling is
very bad. Is this assumption correct? If so, what is the better way?
The draw back you're talking about is that execution is switching to
the main thread, which has no real work to do, akin to busy waiting:

http://en.wikipedia.org/wiki/Busy_waiting

I'm not sure what advantages you're getting from the BackgroundWorker
(never used it), but Thread.Join() seems like a good solution for you.
Create thread objects for your worker methods (hanging on to the
references) and then join them all when you want to wait for
completion.

http://msdn.microsoft.com/en-us/library/95hbf2ta.aspx
Jun 27 '08 #2
On Jun 18, 2:52*pm, Mick Wilson <mick.wil...@gmail.comwrote:
On Jun 18, 12:23*pm, keeling <randykeel...@gmail.comwrote:
But as I said, it is my understanding that this sort of polling is
very bad. Is this assumption correct? If so, what is the better way?

The draw back you're talking about is that execution is switching to
the main thread, which has no real work to do, akin to busy waiting:

http://en.wikipedia.org/wiki/Busy_waiting

I'm not sure what advantages you're getting from the BackgroundWorker
(never used it), but Thread.Join() seems like a good solution for you.
Create thread objects for your worker methods (hanging on to the
references) and then join them all when you want to wait for
completion.

http://msdn.microsoft.com/en-us/library/95hbf2ta.aspx
The reason I went with the BackgroundWorker is that it uses the
ThreadPool. Because each worker could use a sizable amount of memory,
I want to be able to limit how many are firing at any given time. With
the ThreadPool I can limit this number while the waiting workers
remain queued. In other words I can can create all the workers and
forget about them because the ThreadPool will handle the queue.
Jun 27 '08 #3
On Jun 18, 3:06*pm, keeling <randykeel...@gmail.comwrote:
The reason I went with the BackgroundWorker is that it uses the
ThreadPool. Because each worker could use a sizable amount of memory,
I want to be able to limit how many are firing at any given time. With
the ThreadPool I can limit this number while the waiting workers
remain queued. In other words I can can create all the workers and
forget about them because the ThreadPool will handle the queue.- Hide quoted text -
This example from MSDN shows how to signal the Main thread from a
Thread pool using the WaitHandle class. Each worker will signal when
its done (via manualEvent.Set();) and Main will proceed only when all
threads have signaled (via WaitHandle.WaitAll(manualEvents);)

http://msdn.microsoft.com/en-us/library/z6w25xa6.aspx
Jun 27 '08 #4

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

Similar topics

12
by: Gurpreet Sachdeva | last post by:
I have written a code to figure out the difference in excecution time of a func before and after using threading... #!/usr/bin/env python import threading import time loops =
3
by: ron | last post by:
If I use the System.Threading.ThreadPool.QueueUserWorkItem to spawn a new threaded process, will this process be executed regardless of whether or not the current process ends prior to completion...
77
by: Jon Skeet [C# MVP] | last post by:
Please excuse the cross-post - I'm pretty sure I've had interest in the article on all the groups this is posted to. I've finally managed to finish my article on multi-threading - at least for...
11
by: Mark Rae | last post by:
Hi, My R&D department has asked me to look at threading in a Web Service written in C#, so I came up with the following code: using System; using System.ComponentModel; using...
3
by: Asad | last post by:
Hi, I am trying to write some threading code to my application. The reason I've been tempted to do this is because, I am doing some FTP uploads, and sometimes during the put method, the...
6
by: Joe | last post by:
I need to start 8 threads and have the app or calling thread wait until all threads are done. I was thinking of using Join but that will block the thread before any other thread could be started. ...
12
by: Perecli Manole | last post by:
I am having some strange thread synchronization problems that require me to better understand the intricacies of Monitor.Wait/Pulse. I have 3 threads. Thread 1 does a Monitor.Wait in a SyncLock...
2
by: akameswaran | last post by:
Admittedly this problem causes no actual functional issues aside from an occasional error message when the program exits. The error is: Unhandled exception in thread started by Error in...
5
by: bean330 | last post by:
Hey, I'm somewhat new to C# and I need a little help, please! I'm selecting a bunch of records, setting properties on a COM executable and then calling a method on that executable to run. I...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
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,...
0
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
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
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...
0
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...
0
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,...

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.