Reputation: 73
When I use SynchronizedCollection
, I can catch the exception
System.InvalidOperationException
Collection was modified; enumeration operation may not execute.
in foreach
cycle.
If you look at the source code of SynchronizedCollection class, namely in GetEnumerator method (there is two of them actually one is explicit interface implementation), you’ll see this:
List<T> items;
IEnumerator IEnumerable.GetEnumerator()
{
return ((IList)this.items).GetEnumerator();
}
public IEnumerator<T> GetEnumerator()
{
lock (this.sync)
{
return this.items.GetEnumerator();
}
}
It returns enumerator of inner List which is not thread safe!
Edit. I am asking not what can I do in concurrent situations. I am asking
How to make it thread safe?
Upvotes: 0
Views: 212
Reputation: 73
I used SynchronizedCollection class and used foreach (var element in this.items) yield return element;
here
public new IEnumerator GetEnumerator()
used constructors from base classes and that was enough to make it thread-safe
Upvotes: 0
Reputation: 11
I think there is two solution for this:
The second one is much safer, in the cost of performance.
If you are using .NetCore, ImmutableList<T> is a better way for snap shot, and can avoid shallow copy problem.
Upvotes: 1