[英]How to dispose the resource when it might be used by multiple clients?
我正在实现一种流水线模式,该资源(例如一个图像对象)将通过该流水线,而且我不知道有多少客户端持有该资源,那么配置该资源的更好方法是什么?
I think you only really have two options. 我认为您真的只有两个选择。
1) Someone owns that object, and controls access to it. 1)有人拥有该对象,并控制对它的访问。 Therefore, it must dispose of it when it has determined that no one should need it anymore. 因此,当它确定不再需要它时,必须将其丢弃。 This is good old manual resource management. 这是很好的旧的手动资源管理。
2) You don't dispose it, and have to wait for the GC to collect and call the finalizer on the instance. 2)您不需要处理它,而必须等待GC收集并在实例上调用终结器。 Any object that implements IDisposable should also have a finalizer that handles the sam elogic if Dispose is not called. 如果未调用Dispose,则实现IDisposable的任何对象还应具有处理sam elogic的终结器。 If you call dispose, then this extra step does not have to be taken, and GC is more efficient. 如果调用dispose,则不必执行此额外步骤,并且GC效率更高。
The way that springs immediately to mind is to wrap the resource inside some form of reference counting "wrapper" so that you can dispose of it when the reference count has released zero. 立即想到的方式是将资源包装在某种形式的引用计数“包装器”中,以便您可以在引用计数释放为零时处置它。
Something like this: 像这样:
public class RefCountingWrapper<T> where T:IDisposable, new()
{
private int referenceCount = 0;
private T resource;
public RefCountingWrapper(T item)
{
resource = item;
}
public T Acquire()
{
referenceCount++;
return resource;
}
public void Release()
{
referenceCount--;
if (referenceCount <= 0)
{
resource.Dispose();
}
}
}
Caveats that apply: 适用的注意事项:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.