简体   繁体   English

如何在线程返回池之前阻塞?

[英]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) 作为Windows服务的一部分,我接受使用myListener.BeginAcceptSocket(acceptAsync, null)传入套接字连接

The acceptAsync function executes on a seperate thread (just as expected). acceptAsync函数在单独的线程上执行(正如预期的那样)。

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. 我有一个线程列表,我认为我可以迭代并Join每个线程,直到它们全部完成。

Howerver it seems that these threads don't end, but return to the pool, so the Join will wait for ever. 似乎这些线程似乎没有结束,但返回到池中,因此Join将永远等待。

How do I block until a thread is returned to the pool? 如何在线程返回池之前阻塞?

You shouldn't use Join in this case. 在这种情况下,您不应该使用Join。 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. 相反,您应该使用一系列WaitHandles(特别是AutoResetEvent或ManualResetEvent),线程将在完成工作时发出信号。

You would then call the static WaitAll method on the WaitHandle class, passing all of the events to wait on. 然后,您将在WaitHandle调用静态WaitAll方法 ,将所有事件传递给等待。

The canonical pattern for doing this is to use a CountdownEvent . 执行此操作的规范模式是使用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 . 如果您不使用.NET 4.0,那么您可以从Joe Albahari的线程电子书的第4部分获得倒计时事件的实现。

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();
    }
  }
}

The best way would be to change acceptAsync so that it signals on a semaphore, your main thread can then wait on that semaphore. 最好的方法是更改acceptAsync以便它在信号量上发出信号,然后您的主线程可以在该信号量上等待。

You don't have a lot of acces to or control over Threapool threads. 您没有很多访问或控制Threapool线程的权限。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM