如果我用信号量控制一个资源池,这个资源池的干净关闭顺序是什么?
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,这最终应该发挥作用。