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,這應該最終發揮作用。