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