Click here to Skip to main content
Click here to Skip to main content

Concurrent Observable Collection

By , 11 Apr 2012
 

Introduction

There are plenty of custom implementations of thread safe observable collections. Some are more advanced than others but in most scenarios these are overkill. The overhead introduced by concurrency code in most cases is not necessary. I will demonstrate how the regular ObservableCollection could be used in a multithreaded environment without any need for creating a new type.

Background

In order to use a collection in a thread safe manner, at the very least, there has to be an object which could be used to synchronize access to the collection. Most custom solutions subclass the ObservableCollection in order to associate a sync object with the collection but it is not necessary. ObservableCollection already has that object. It derives from the Collection class which implements the ICollection interface.

public interface ICollection : IEnumerable
{
    int Count { get; }
    bool IsSynchronized { get; }
    object SyncRoot { get; }
    void CopyTo(Array array, int index);
}

The SyncRoot property gives us exactly the object we need. Now synchronization of the collection is only a matter of properly applying locks only where and when it is required. There is no point in wasting any processing time where it is not needed.

Using the code

Using the collections in a thread safe manner is very simple. Just cast it into ICollection and lock on the SyncRoot property:

lock ((_collection as ICollection).SyncRoot)
{
    _collection.Add(newObject);
}

SyncRoot provides a universal way of accessing the synchronization object of the collection. So if you subclass a collection and create your own type, you will still be able to access and use this object and work in concert with whatever synchronization the class provides.

Implementing Concurrency in a derived type

If you wish to implement a thread safe collection as a derived type, it is still beneficial to use the SyncRoot property. It gives you access to the same lock object inside and outside your class, and allows extra flexibility in handling non-trivial situations.

lock((this as ICollection).SyncRoot)
{
    T removedItem = this[oldIndex];
    base.RemoveItem(oldIndex);
    base.InsertItem(newIndex, removedItem);
}

Because lock() internally uses Monitor, it is safe to call lock on the same thread recursively. This allows us to combine several operations under the same external lock:

lock ((_collection as ICollection).SyncRoot)
{
    T removedItem = _collection[Index];
    _collection.RemoveAt(oldIndex);
    _collection.Insert(newIndex, removedItem);
}

When lock is applied to SyncRoot inside RemoveAt, Insert, etc., it will not block. It will increase the reference counter and continue with the operation. Once the operation is done, it will decrease the counter and release the lock once all of them are done.

History

  • 10/27/2011 - Released.
  • 04/11/2012 - Recategorized as Tip

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

Eugene Sadovoi
Software Developer (Senior)
United States United States
Member
Senior Software Engineer with over 20+ years of experience in variety of technologies, development tools and programming languages.
 
Microsoft Certified Specialist programming in C#, JavaScript, HTML, CSS

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
Hint: For improved responsiveness ensure Javascript is enabled and choose 'Normal' from the Layout dropdown and hit 'Update'.
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
AnswerRe: Concurrent CollectionsmemberEugene Sadovoi8 Nov '11 - 3:43 
You are right. There are right tool for every job. I am using collections from concurrent namespace all the time. The case I describe in my article is rather exception than the common solution.
GeneralMy vote of 4memberSledgeHammer0128 Oct '11 - 7:49 
Convienient, but not really "nice" to have your code littered with lock ((_collection as ICollection).SyncRoot) calls .
AnswerRe: My vote of 4 [modified]memberEugene Sadovoi28 Oct '11 - 7:52 
It does not have to be outside your type. It might be if you require it. You could use this object in derived type to implement concurrency.   Most of the concurrent solutions I've seen so far implement its own instance of SyncRoot. I am trying to point out the fact that standard...
QuestionusefulmemberCIDev28 Oct '11 - 6:25 
This is clearly written and useful; although rather short for an article, more the size of a tip. Just because the code works, it doesn't mean that it is good code.
AnswerRe: usefulmemberEugene Sadovoi28 Oct '11 - 7:24 
It is a bit shorter than I anticipated. I was going to give few examples with events and dispatcher but decided that it will only distract from the main idea.
GeneralEventsmemberstooboo27 Oct '11 - 23:34 
The subclassing that I've seen for this usually includes raising the collections events on a specific thread ...(normally the UI thread) So although this will work it doesn't address all the issues
GeneralRe: EventsmemberEugene Sadovoi28 Oct '11 - 4:05 
The focus of this article is this little known way of synchronizing regular collections. Proper handling of events is too complex an issue and beyond this article.
GeneralRe: Eventsmemberstooboo28 Oct '11 - 7:39 
Ok , but I think that most people choosing to use ObservableCollection (rather than other collection classes) will do so because of the CollectionChanged event   So which thread that event will actually be raised on is an important consideration if your are going to use it in a...
GeneralRe: EventsmemberEugene Sadovoi28 Oct '11 - 7:49 
stooboo,   You are absolutely right. This topic is very important and a lot of people dedicated lots of time explaining it. Simple search for PropertyChanged and UI[^] returns several pages of useful information on this topic. I should have mentioned in this article that I left this...
QuestionSynchronization needs to be handled with carememberJohn Brett27 Oct '11 - 22:43 
I think this article underplays the issue of synchronization, which is potentially one of the most complex we face as developers.   The Framework developers themselves are uneasy with the SyncRoot pattern, and have dropped it from the generic collections - this is the reason why you need...
AnswerRe: Synchronization needs to be handled with carememberJulien Villers27 Oct '11 - 23:09 
The article you've linked to doesn't oppose this article's approach. It says that the Framework itself won't be using much SyncRoot/IsSynchronized as it's not efficient for many non-trivial cases. But using it from outside the class as is done here isn't discouraged, at least by the article...
AnswerRe: Synchronization needs to be handled with carememberEugene Sadovoi28 Oct '11 - 4:29 
John,   I do not agree with your statement about synchronization being that complex. There are no magic involved and if you follow few well known basic principles it is rather trivial. This pattern allows you access to the sync object native to the collection. It means that event if...
QuestionThoughtmembercdkisa27 Oct '11 - 14:42 
K.I.S.S. well stated.
AnswerRe: ThoughtmemberEugene Sadovoi28 Oct '11 - 7:36 
Thank you for your feedback!

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Permalink | Advertise | Privacy | Mobile
Web04 | 2.6.130516.1 | Last Updated 11 Apr 2012
Article Copyright 2011 by Eugene Sadovoi
Everything else Copyright © CodeProject, 1999-2013
Terms of Use
Layout: fixed | fluid