About threading? How to know if threads finished?

D

Diego F.

Hello. I'd like to know if there's a way to know if all threads started
using the threadpool are finished.

How can I ensure that all threads finished?
 
M

Marc Gravell

Your own pooled threads would have to let you know somehow... perhaps
a synchronised counter? and if your threads Pulse when decrementing
you should be able to simulate a Join (to all):

private static int threadCount;
private static readonly object threadLock = new object();
private static void ThreadStarted() {
lock (threadLock) {
threadCount++;
}
}
private static void ThreadEnded() {
lock (threadLock) {
threadCount--;
if(threadCount<=0) {
Monitor.Pulse(threadLock);
}
}
}
private static void WaitForAll() {
lock (threadLock) {
while (threadCount > 0) {
Monitor.Wait(threadLock);
}
}
}
private void ThreadMethod() {
ThreadStarted();
try {
// do somthing
} finally {
ThreadEnded();
}
}
 
M

Marc Gravell

or more conveniently... (note I have not either approach myself...
just thinking aloud...)
private static void ThreadMethod() {
using (new ThreadWatcher()) {
// do something
}
}
private sealed class ThreadWatcher : IDisposable {
private bool disposed;
public ThreadWatcher() {
ThreadStarted();
}
void IDisposable.Dispose() {
if (!disposed) {
ThreadEnded();
disposed = true;
}
}
// perhaps move the related methods / fields for start / end /
wait method here too...
}

Marc
 
D

Diego F.

What I'm looking for is a way of blocking the main thread until all threads
in ThreadPool are finished.

Is that possible with ThreadPool object?
 
M

Marc Gravell

*all* threads? Or those that you started?

Since other code could bne using the ThreadPool, I wouldn't recommend
the first and I doubt that this is available. It seems very risky,
certainly; you should only be interested in threads that you own
(pooled or otherwise). For the second, see the WaitForAll() method
that I posted; it does exactly that - just don't call it on a pool
thread!

Marc
 
D

Diego F.

If I understand you, you suggest not using the ThreadPool, and starting my
own threads instead. Is that rigth?
 
M

Marc Gravell

I never mentioned anything about creating threads... all I am saying
is that your worker methods (be they thread-pool or otherwise) could
notify *something* to indicate that they are complete. I have just
tested it, and it "works OK for small values of works" - there is a
glitch that you can exit too soon if the pool item hasn't started yet
(and thus the counter is 0).

Another approach is to use a dedicated pool; I believe Jon has some
template code in his box of tricks:
http://www.yoda.arachsys.com/csharp/miscutil/

(see CustomThreadPool)

This should allow you to track when it is empty... or allow simple
customisation

Marc
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top