views:

98

answers:

1

If i control a pool of resources 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.

A: 

Hope that helps Thread Management In The CLR

lsalamon