Ralph Shillington
Ralph Shillington

Reputation: 21098

How do I block until a thread is returned to the pool?

As part of a windows service I'm accepting incoming socket connection using myListener.BeginAcceptSocket(acceptAsync, null)

The acceptAsync function executes on a seperate thread (just as expected).

When the service is requested to shutdown, I "signal" the threads that accepted and are currently working on the sockets, to finish up.

After signaling each thread to end,I need to block until they are all done. I have a list of threads, that I thought I could iterate through and Join each thread until they were all done.

Howerver it seems that these threads don't end, but return to the pool, so the Join will wait for ever.

How do I block until a thread is returned to the pool?

Upvotes: 3

Views: 184

Answers (3)

Brian Gideon
Brian Gideon

Reputation: 48949

The canonical pattern for doing this is to use a CountdownEvent. The main thread will increment the event to indicate that it is participating and the worker threads will do the same once they start. After the worker threads have finished they will decrement the event. When the main thread is ready to wait for completion it should decrement the event and then wait on it. If you are not using .NET 4.0 then you can get an implemention of a countdown event from part 4 of Joe Albahari's threading ebook.

public class Example
{
  private CountdownEvent m_Finisher = new CountdownEvent(0);

  public void MainThread()
  {
    m_Finisher.AddCount();

    // Your stuff goes here.
    // myListener.BeginAcceptSocket(OnAcceptSocket, null);

    m_Finisher.Signal();
    m_Finisher.Wait();
  }

  private void OnAcceptSocket(object state)
  {
    m_Finisher.AddCount()
    try
    {
      // Your stuff goes here.
    }
    finally
    {
      m_Finisher.Signal();
    }
  }
}

Upvotes: 3

Henk Holterman
Henk Holterman

Reputation: 273169

The best way would be to change acceptAsync so that it signals on a semaphore, your main thread can then wait on that semaphore.

You don't have a lot of acces to or control over Threapool threads.

Upvotes: 1

casperOne
casperOne

Reputation: 74530

You shouldn't use Join in this case. Rather, you should use a series of WaitHandles (specifically, an AutoResetEvent or ManualResetEvent) which your threads will signal when they are done with their work.

You would then call the static WaitAll method on the WaitHandle class, passing all of the events to wait on.

Upvotes: 4

Related Questions