

Concurrent collections are a feature added in .Net 4.0 and allow developers to create thread safe collections without worrying as much as they had to worry with Generic Collections.

They do not remove any possible concern the developer should have while working with resources that are accessed from multiple threads, but removes many of the common ones.

Now we have many powerful collections in .Net and especially Generic ones that are really fast and really convenient, but none of the ones in the System.Collections.Generic namespace are thread-safe.

So what would we do if we wanted to have multi-threaded access to a resource?

Use locks of course.

For example to add a key value pair in a dictionary we would implement a wrapper method like the following

private Dictionary m_dictionary = new Dictionary (); private object m_lockObject = new object(); public void Add(int key, string value) { lock(m_lockObject) { m_dictionary.Add(key,value); } }

And after this we would hope that nobody violates this, because any other developer could actually mistakenly directly try to access the dictionary and add a key value pair without the use of the above method.

But developers do not rely on hopes and dreams because they always get shattered.

So what would we do? we would create a wrapper class that wraps around the whole dictionary and exposes only thread-safe methods to any possible user. All those thread-safe methods would be implemented using locks. Usually this would be done on a separate dll or namespace to encapsulate the internal collection even better.

In fact Microsoft did something similar when implementing SynchronizedCollection. In fact, SynchronizedCollection is simply a wrapper around List that uses locks whenenever there is need to access the lists data.

But wouldn’t that actually hinder performance and make the parallel version of our code run even slower than the serial one? Wouldn’t that make our threads so slow that they would wait for so long when locking in high parallelization paradigms?

YES IT WOULD

So, in comes .Net 4.0 and Microsofts new implementation of concurrent collections, under a new namespace, and with a promise of a combination efficiency and thread-safety.

So what do we have inside this namespace?

1. ConcurrentDictionary

2. ConcurrentStack

3. ConcurrentQueue

4. ConcurrentBag

5. BlockingCollection

So let’s see how to use each one.

ConcurrentCollections

static void Main(string[] args) { ConcurrentDictionary dict = new ConcurrentDictionary (); bool firstItem = dict.TryAdd("1", "First"); bool secondItem = dict.TryAdd("2", "Second"); string itemValue1; string itemValue2; bool doesItemOneExist = dict.TryGetValue("1", out itemValue1); //returns true Console.WriteLine(itemValue1); //Print "First" bool doesItemTwoExist = dict.TryGetValue("3", out itemValue2); //returns false Console.WriteLine(itemValue2); //Print null }

As you see concurrent dictionaries (and in fact all concurrent collections) utilize the TryPattern in which safeguards against failures, returns true or false whether the operation was successful or not and guarantees atomicity of each operation.

What is atomicity? Well we say an operation is atomic when it completes within a single step without being interrupted. Only atomic operations are thread-safe. Code inside a locking block is by definition atomic. Any other code that has no guarantee of atomicity is not in fact atomic (even methods of .Net framework and C# itself).

ConcurrentQueue

Similarly a concurrent queue is the concurrent implementation of queue and it uses the TryPattern.

The following code is using ConcurrentQueue in a multi-threaded operation.

static void Main(string[] args) { ConcurrentQueue nameCollection = new ConcurrentQueue (); nameCollection.Enqueue("Luke Skywalker"); nameCollection.Enqueue("Leia Organa"); nameCollection.Enqueue("Han Solo"); nameCollection.Enqueue("Chewbacca"); nameCollection.Enqueue("Obi-Wan Kenobi"); nameCollection.Enqueue("Lando Calrissian"); Task[] taskList = new Task[2]; taskList[0] = Task.Factory.StartNew(() => { string name = String.Empty; while (nameCollection.TryDequeue(out name)) { Console.WriteLine("Name {0} is dequeued!", name); } }); taskList[1] = Task.Factory.StartNew(() => { string name = String.Empty; while (nameCollection.TryDequeue(out name)) { Console.WriteLine("Name {0} is dequeued!", name); } }); Task.WaitAll(); Console.ReadLine();

As you can see in the above example I do not always use the try methods. In parts of code where I know that no other thread/task will access the resource I can use the “non-thread-safe” methods instead of the thread-safe version. This should be done only in cases where you are 100% positive that no other task will simultaneously access the collection. So if in doubt, always use the thread-safe methods.

And in this example again you also see that we are using the TryPattern to remove from queue and the return value to see if anything else is left inside the queue.

ConcurrentStack

Similarly a concurrent Stack is a concurrent version of the stack we all know. You can perform multithreaded LIFO access to data with the same pattern.

static void Main(string[] args) { ConcurrentStack numberCollection = new ConcurrentStack (); for (int i = 10; i <= 20; i++) { numberCollection.Push(i); } Parallel.For(0, 10, i => { int value = 0; if (numberCollection.TryPop(out value)) { Console.WriteLine("Value {0} is popped out!", value); } }); Console.ReadLine(); }

Again in parts of the code where guaranteed single threaded execution exists, there is no reason to use the multi-threaded option.

ConcurrentBag

The final part of our list of concurrent paradigms that .Net is giving us out of the box is the concurrent bag.

Concurrent bag is used to store unordered items to be processed. In cases that we do not care about the order items come and will be processed but we want to be multi-threaded safe we use concurrent bag.

private static ConcurrentBag m_entityCollection = new ConcurrentBag (); static void Main(string[] args) { m_entityCollection.Add("Ripple"); m_entityCollection.Add("Bitcoin"); m_entityCollection.Add("Litecoin"); m_entityCollection.Add("OmiseGO"); m_entityCollection.Add("Ethereum"); m_entityCollection.Add("Swissborg"); m_entityCollection.Add("Tron"); m_entityCollection.Add("Monero"); ThreadStart threadStart = new ThreadStart(PrintEntities); Thread thread1 = new Thread(threadStart); thread1.Start(); Thread thread2 = new Thread(threadStart); thread2.Start(); Console.ReadLine(); } static void PrintEntities() { string crypto; while (m_entityCollection.TryPeek(out crypto)) { if (m_entityCollection.TryTake(out crypto)) { Console.WriteLine(entity); } }

Two unique methods that come with ConcurrentBag are TryPeek and TryTake

They both adhere to the try pattern.

The differences are that TryPeek tries to get a value and returns whether it was successful or not, while TryTake also removes the value from the Bag.

That’s it for Concurrent Collections in the .Net Framework.

Use them wisely