0

如果我用信号量控制一个资源池,这个资源池的干净关闭顺序是什么?

  class ResourcePool
  {
  Semaphore resourceSemaphore;
  Stack<ResourceClass> resources;
    public ResourcePool()
    {
        resources ... // Init some Resources in the stack
        resourceSemaphore=  new Semaphore(resources.Count,resources.Count);

    }

    public void ResourceTask()
    {
        resourceSemaphore.WaitOne();
        ResourceClasscurrent = null;
        try
        {
            current = resources.Pop();
            current.Task();
        }
        finally
        {
            if( current != null )
                resources.Push(current);

            resourceSemaphore.Release();
        }

    }
  }

如何为此池实施干净的关闭顺序?也许资源使用 IDisposable,这最终应该发挥作用。

4

1 回答 1

0

希望对 CLR 中的线程管理有所帮助

于 2009-10-21T14:26:41.167 回答