困惑,可能遗漏了一些简单的东西..
我有
var q = new Queue<object>();
lock (q.SyncRoot)
{
...
}
我明白了
Queue<T> does not provide a defintion for SyncRoot blah blah...
但是,Queue<T>
工具 ICollection
它定义了 SyncRoot
作为公共(public)属性(property)。
所以,首先,为什么这是隐藏的。其次,如何隐藏正在实现的接口(interface)的属性?
最佳答案
您可以通过 implementing it explicitly 隐藏属性:
object MyQueue.SyncRoot { get; set; }
We found the SyncRoot-based synchronization APIs to be insufficiently flexible for most scenarios. The APIs allow for thread safe access to a single member of a collection. The problem is that there are numerous scenarios where you need to lock on multiple operations (for example remove one item and add another). In other words, it’s usually the code that uses a collection that wants to choose (and can actually implement) the right synchronization policy, not the collection itself. We found that SyncRoot is actually used very rarely and in cases where it is used, it actually does not add much value. In cases where it’s not used, it is just an annoyance to implementers of ICollection.
关于c# - SyncRoot 如何/为什么隐藏在 Queue<T> 上?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12746772/