ThreadPool question .NET 2

B

Brian Stoop

I have several Threads that start other Threads like this:

ThreadA
{
for (int i=0; i <10; i++)
{
ThreadPool.QueueUserWorkItem(new WaitCallback(TestThread1),
(object)i);
}

// Wait for all TestThread1 to terminate before proceeding ??????
}

ThreadB
{
for (int i=0; i <10; i++)
{
ThreadPool.QueueUserWorkItem(new WaitCallback(TestThread2),
(object)i);
}

// Wait for all TestThread2 to terminate before proceeding ??????
}


--------------------------------------------------------------------------------------------------

The TestThread1 and TestThead2 methods have as a randon sleep up to 30
seconds before terminating.

How can I tell when all the ThreadA ThreadPool requests have completed ?

thanks B
 
S

Shree

I have several Threads that start other Threads like this:

ThreadA
{
    for (int i=0; i <10; i++)
    {
        ThreadPool.QueueUserWorkItem(new WaitCallback(TestThread1),
(object)i);
    }

    // Wait for all TestThread1 to terminate before proceeding ??????

}

ThreadB
{
    for (int i=0; i <10; i++)
    {
        ThreadPool.QueueUserWorkItem(new WaitCallback(TestThread2),
(object)i);
    }

    // Wait for all TestThread2 to terminate before proceeding ??????

}

--------------------------------------------------------------------------------------------------

The TestThread1 and TestThead2 methods have as a randon sleep up to 30
seconds before terminating.

How can I tell when all the ThreadA ThreadPool requests have completed ?

thanks B

A simple way could be to keep a static counter which can be updated in
the call back function "TestThread1". Make the main thread wait while
this counter is not maxed out; 10 in your case.
 
B

Brian Stoop

Is there a way to get this informaton from the ThreadPool ?

I have several Threads that start other Threads like this:

ThreadA
{
for (int i=0; i <10; i++)
{
ThreadPool.QueueUserWorkItem(new WaitCallback(TestThread1),
(object)i);
}

// Wait for all TestThread1 to terminate before proceeding ??????

}

ThreadB
{
for (int i=0; i <10; i++)
{
ThreadPool.QueueUserWorkItem(new WaitCallback(TestThread2),
(object)i);
}

// Wait for all TestThread2 to terminate before proceeding ??????

}

--------------------------------------------------------------------------------------------------

The TestThread1 and TestThead2 methods have as a randon sleep up to 30
seconds before terminating.

How can I tell when all the ThreadA ThreadPool requests have completed ?

thanks B

A simple way could be to keep a static counter which can be updated in
the call back function "TestThread1". Make the main thread wait while
this counter is not maxed out; 10 in your case.
 
W

William Stacey [C# MVP]

Things like the Parallel fx gives you ability to scatter and gather (i.e.
wait for all or any) which makes these kind of patterns easier. It is also
a good library to get to know and worth the learning curve for future
projects.

Brian Stoop said:
I have several Threads that start other Threads like this:

ThreadA
{
for (int i=0; i <10; i++)
{
ThreadPool.QueueUserWorkItem(new WaitCallback(TestThread1),
(object)i);
....
 
B

Brian Stoop

thanks.

Peter Duniho said:
Not really. Even if you could get the ThreadPool to tell you how many
busy threads there are, there could be other ThreadPool threads working as
well. There'd be no way to know that as the current count of busy
ThreadPool threads changed, that was because of your threads versus any
others.

If you follow Shree's advice, make sure you increment and decrement your
counter in a thread-safe way (e.g. see the Interlocked class) and you
don't "busy wait" on the counter (i.e. create a single AutoResetEvent that
each worker thread sets after changing the counter, and have the main
thread wait on that handle before checking the counter).

Other alternatives include giving each thread a unique WaitHandle object
(again, an AutoResetEvent would work fine), having the main thread wait on
all of them, with the worker threads setting the handle when they are
done; or, combine the two techniques with a counter that the threads
manage and a single WaitHandle that the main thread waits on, and the last
worker thread to complete would set.

IMHO the last solution is the best, as it minimizes OS resources used
(just one AutoResetEvent object) but doesn't bother the main thread until
it's guaranteed to be able to proceed (it's silly to keep waking the main
thread up just to check a counter that an already-running thread could
have checked).

Pete
 

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