开发者

Semaphore controlled resources - what is the clean shutdown sequence/pattern

If i control a pool of res开发者_高级运维ources with a semaphore, what is the clean shutdown sequence for this resource pool?

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

    }
  }

How to implement a clean shutdown sequence for this pool? Maybe the resources use IDisposable, this should eventually come into play.


Hope that helps Thread Management In The CLR

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜