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

 
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
QuestionConcurrent CollectionsmemberYandyZM31 Oct '11 - 11:21 
What is the diferent betwen this and the concurrent collections in System.Collections.Concurrent like ConcurrentDictionary, ConcurrentQueue, ConcurrentBag, ConcurrentStack, etc. Is this solution better? Or you don't know anything about the Task Parallel Library?
 
In C++, the equivalents are concurent collections for: vector and stack, and in visual studio 11 they add: queue, priorityqueye and map.
AnswerRe: Concurrent Collections [modified] PinmemberEugene Sadovoi31 Oct '11 - 13:44 
YandyZM,
 
I lot of code uses ObservableCollection and Collection classes because it performs much faster than collections from Concurrent namespace. There are other differences as well. For all proctical purposes these are completely different types of collections with different design goals.
 
My intention was not to come up with the best solution for concurrent collection but rather to show how to safelly use regular ObservableCollection and Collection in multithreaded environment.

modified 31 Oct '11 - 21:22.

GeneralRe: Concurrent Collections PinmemberYandyZM1 Nov '11 - 4:43 
Ok, I going to explain you where i use concurrent collections, and please tell me if you think it will perform better using a custom thread safe collection and why.
 
I'm using threads for the process thread pool to get notifications for IO completed tasks. Then i pick up this tasks and enqueue they in a concurrent collection from the concurrent namespace, to procees they later in other threads. Before change to concurrent collections i was using a custom locked collection but i had the feeling that it was creating a bottle neck.
 
From my point of view, the concurrent collections in the concurrent namespace are the right choice. And suposing you create your own concurrent collection and make it work correctly and efficiently through Interlocked operations and ensuring they don't suffer from the AB problem, you will end up getting the same performance.
 
Now, there exist some situations where and inner locking mechanism like those from concurrent collections it is not enough, and you will end up doing external lockings and having the innecesary lock from the concurrent collections. But, for the right task, the concurrent collections from the concurrent namespace are the right choice. Correct me if i am wrong.
AnswerRe: Concurrent Collections PinmemberEugene 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.

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

Permalink | Advertise | Privacy | Mobile
Web02 | 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