14

Everyone use lot of List. I need to iterate over this list, so I use the known SyncRoot pattern.

Recently I noticed in this post that the SyncRoot should be avoided in favor of "embedded" thread-safety (each method will lock on an private object without exposing it using SyncRoot property). I can understand it, and partially I agree on that.

The question is that List<T> class doesn't implements the SyncRoot property, even if implements the ICollection interface, which expose the SyncRoot property. I say this bause the code

 List<int> list = new List<int>()
 list.SyncRoot;

give me the following compiler error:

error CS0117: 'System.Collections.Generic.List' does not contain a definition for 'SyncRoot'

...If this is true, how could I synchronize a public property of type List<T> when iterating over it?

Community
  • 1
  • 1
Luca
  • 11,646
  • 11
  • 70
  • 125
  • 1
    I have never found SyncRoot to work well in real life, as locking tends to work better at a higher level, rather then just locking single collections – Ian Ringrose Nov 01 '10 at 09:04

1 Answers1

20

It is actually implemented explicitly.

object ICollection.SyncRoot
{
    get
    {
        if (this._syncRoot == null)
        {
            Interlocked.CompareExchange(ref this._syncRoot, new object(), null);
        }
        return this._syncRoot;
    }
}

This means you must cast to ICollection to use it.

ChaosPandion
  • 77,506
  • 18
  • 119
  • 157
  • 1
    Great, it is correct. I didn't know about explicit implementations, thanks. – Luca Nov 01 '10 at 08:01
  • Could it be advisable to have an explicit implementation of SyncRoot property even on my own classes? (I have custom implementations of collections) I think it could discourage the SyncRoot usage. – Luca Nov 01 '10 at 08:10
  • 3
    The use of SyncRoot is indeed discouraged in for that reason it is implemented explicitly. I find implementing members explicitly useful especially in two scenario's: 1. The use of the member has no use outside of the interface (see for instance this example: http://bit.ly/crLXAz) 2. The member has not been implemented and throws an exception (see for instance this example http://bit.ly/3RFzn4 where an `Add` method has no meaning on a `ReadOnlyDictionary`). – Steven Nov 01 '10 at 08:17
  • 7
    @Luca - Personally I would avoid relying on `SyncRoot` and instead use the new `System.Collections.Concurrent.BlockingCollection` class. This can help reduce errors because you don't have to lock every action against the list. – ChaosPandion Nov 01 '10 at 08:18