Thursday, April 28, 2016


Thread-Safe collections in C#

In this article, we will learn:

  • What is a Concurrent collection in C# ?
  • BlockingCollection<T>
  • ConcurrentBag<T>
  • ConcurrentDictionary<TKey,T>
  • ConcurrentStack<T>
  • ConcurrentQueue<T>
  • Summary

What is a Concurrent collection in C# ?

While working in a multithreaded environment, we need to make sure that we are not manipulating shared data at the same time without synchronizing access.
The .NET Framework offers some collection classes to use in concurrent environments. Those are:
Thread Safe collection
These collections are thread-safe, that means they internally use synchronization to make sure that they can be accessed by multiple threads at the same time.

BlockingCollection<T> :

This collection is mainly used for adding and removing data.
Removing an item from the collection can be blocked until data becomes available.
Adding data is fast, but you can set a maximum upper limit. If that limit is reached, adding an item blocks the calling thread until there is room.
BlockingCollection is actually a wrapper around other collection types. If you don’t give it any specific instructions, it uses the ConcurrentQueue by default.

Working with BlockingCollection<T> :

The program terminates when the user doesn’t enter any data.
Until that, every string entered is added by the write Task and removed by the read Task.
You can use the CompleteAdding method to signal to the BlockingCollection that no more items will be added.

ConcurrentBag<T> :

A ConcurrentBag is a bag of items. It enables duplicates and it has no particular order.The important methods in Concurrentbag are Add, TryTake, and TryPeek.

Working with ConcurrentBag<T> :


There is a next item : 53
Note, TryPeek method is not very useful in a multithreaded environment. It could be that another thread removes the item before you can access it
ConcurrentBag also implements IEnumerable, so you can iterate over it.
This operation is made thread-safe by making a snapshot of the collection when you start iterating it, so items added to the collection after you started iterating it won’t be visible.

Enumerating a ConcurrentBag:


This program will print 53 because the other value is added after iterating over the bag has started.

ConcurrentDictionary<T> :

A ConcurrentDictionary stores key and value pairs in a thread-safe manner. You can use methods to add and remove items, and to update items in place if they exist.

Working with ConcurrentDictionary<Tkey,T> :


This program will print:
53 updated to 12

ConcurrentStack<T> :

A stack is a last in, first out (LIFO) collection.
ConcurrentStack has two main methods: Push and TryPop.
Push is used to add an item to the stack and TryPop is used to get an item off the stack.
You can never be sure whether there are items on the stack because multiple threads might be accessing your collection at the same
You can also add and remove multiple items at once by using PushRange and TryPopRange.

Working with ConcurrentStack<T> :


Popped: 53

ConcurrentQueue<T> :

A queue is a first in, first out (FIFO) collection.
ConcurrentQueue has 2 main methods: Enqueue and TryDequeue.
Enqueue and TryDequeue is used to add and remove items from the collection.
It also has a TryPeek method and it implements IEnumerable by making a snapshot of the data.

Working with ConcurrentQueue<T> :


Dequeued: 53


In this article, we have discussed:
  • What is a Concurrent collections in C# ?
  • BlockingCollection<T>
  • ConcurrentBag<T>
  • ConcurrentDictionary<TKey,T>
  • ConcurrentQueue<T>
  • ConcurrentStack<T>
You may also like:
Thanks for visiting !!
© 2016, admin. All rights reserved.

Special thanks to cSharpStar folks

No comments:


HTMLCode Content