Question

I couldn't find enough information on ConcurrentDictionary types, so I thought I'd ask about it here.

Currently, I use a Dictionary to hold all users that is accessed constantly by multiple threads (from a thread pool, so no exact amount of threads), and it has synchronized access.

I recently found out that there was a set of thread-safe collections in .NET 4.0, and it seems to be very pleasing. I was wondering, what would be the 'more efficient and easier to manage' option, as i have the option between having a normal Dictionary with synchronized access, or have a ConcurrentDictionary which is already thread-safe.

Reference to .NET 4.0's ConcurrentDictionary

Was it helpful?

Solution

A thread-safe collection vs. a non-threadsafe-collection can be looked upon in a different way.

Consider a store with no clerk, except at checkout. You have a ton of problems if people don't act responsibly. For instance, let's say a customer takes a can from a pyramid-can while a clerk is currently building the pyramid, all hell would break loose. Or, what if two customers reaches for the same item at the same time, who wins? Will there be a fight? This is a non-threadsafe-collection. There's plenty of ways to avoid problems, but they all require some kind of locking, or rather explicit access in some way or another.

On the other hand, consider a store with a clerk at a desk, and you can only shop through him. You get in line, and ask him for an item, he brings it back to you, and you go out of the line. If you need multiple items, you can only pick up as many items on each roundtrip as you can remember, but you need to be careful to avoid hogging the clerk, this will anger the other customers in line behind you.

Now consider this. In the store with one clerk, what if you get all the way to the front of the line, and ask the clerk "Do you have any toilet paper", and he says "Yes", and then you go "Ok, I'll get back to you when I know how much I need", then by the time you're back at the front of the line, the store can of course be sold out. This scenario is not prevented by a threadsafe collection.

A threadsafe collection guarantees that its internal data structures are valid at all times, even if accessed from multiple threads.

A non-threadsafe collection does not come with any such guarantees. For instance, if you add something to a binary tree on one thread, while another thread is busy rebalancing the tree, there's no guarantee the item will be added, or even that the tree is still valid afterwards, it might be corrupt beyond hope.

A threadsafe collection does not, however, guarantee that sequential operations on the thread all work on the same "snapshot" of its internal data structure, which means that if you have code like this:

if (tree.Count > 0)
    Debug.WriteLine(tree.First().ToString());

you might get a NullReferenceException because inbetween tree.Count and tree.First(), another thread has cleared out the remaining nodes in the tree, which means First() will return null.

For this scenario, you either need to see if the collection in question has a safe way to get what you want, perhaps you need to rewrite the code above, or you might need to lock.

OTHER TIPS

You still need to be very careful when using thread-safe collections because thread-safe doesn't mean you can ignore all threading issues. When a collection advertises itself as thread-safe, it usually means that it remains in a consistent state even when multiple threads are reading and writing simultaneously. But that does not mean that a single thread will see a "logical" sequence of results if it calls multiple methods.

For example, if you first check if a key exists and then later get the value that corresponds to the key, that key may no longer exist even with a ConcurrentDictionary version (because another thread could have removed the key). You still need to use locking in this case (or better: combine the two calls by using TryGetValue).

So do use them, but don't think that it gives you a free pass to ignore all concurrency issues. You still need to be careful.

Internally ConcurrentDictionary uses a separate lock for each hash bucket. As long as you use only Add/TryGetValue and the like methods that work on single entries, the dictionary will work as an almost lock-free data structure with the respective sweet performance benefit. OTOH the enumeration methods (including the Count property) lock all buckets at once and are therefore worse than a synchronized Dictionary, performance-wise.

I'd say, just use ConcurrentDictionary.

I think that ConcurrentDictionary.GetOrAdd method is exactly what most multi-threaded scenarios need.

Have you seen the Reactive Extensions for .Net 3.5sp1. According to Jon Skeet, they have backported a bundle of the parallel extensions and concurrent data structures for .Net3.5 sp1.

There is a set of samples for .Net 4 Beta 2, which describes in pretty good detail on how to use them the parallel extensions.

I've just spent the last week testing the ConcurrentDictionary using 32 threads to perform I/O. It seems to work as advertised, which would indicate a tremendous amount of testing has been put into it.

Edit: .NET 4 ConcurrentDictionary and patterns.

Microsoft have released a pdf called Patterns of Paralell Programming. Its reallly worth downloading as it described in really nice details the right patterns to use for .Net 4 Concurrent extensions and the anti patterns to avoid. Here it is.

Basically you want to go with the new ConcurrentDictionary. Right out of the box you have to write less code to make thread safe programs.

We've used ConcurrentDictionary for cached collection, that is re-populated every 1 hour and then read by multiple client threads, similar to the solution for the Is this example thread safe? question.

We found, that changing it to ReadOnlyDictionary improved overall performance.

The ConcurrentDictionary is a great option if it fulfills all your thread-safety needs. If it's not, in other words of you are doing anything slightly complex, a normal Dictionary+lock may be a better option. For example lets say that you are adding some orders into a dictionary, and you want to keep updated the total amount of the orders. You may write code like this:

private ConcurrentDictionary<int, Order> _dict;
private Decimal _totalAmount = 0;

while (await enumerator.MoveNextAsync())
{
    Order order = enumerator.Current;
    _dict.TryAdd(order.Id, order);
    _totalAmount += order.Amount;
}

This code is not thread safe. Multiple threads updating the _totalAmount field may leave it in a corrupted state. So you may try to protect it with a lock:

_dict.TryAdd(order.Id, order);
lock (_locker) _totalAmount += order.Amount;

This code is "safer", but still not thread safe. There is no guarantee that the _totalAmount is consistent with the entries in the dictionary. Another thread may try to read these values, to update a UI element:

Decimal totalAmount;
lock (_locker) totalAmount = _totalAmount;
UpdateUI(_dict.Count, totalAmount);

The totalAmount may not correspond to the count of orders in the dictionary. The displayed statistics could be wrong. At this point you will realize that you must extend the lock protection to include the updating of the dictionary:

// Thread A
lock (_locker)
{
    _dict.TryAdd(order.Id, order);
    _totalAmount += order.Amount;
}

// Thread B
int ordersCount;
Decimal totalAmount;
lock (_locker)
{
    ordersCount = _dict.Count;
    totalAmount = _totalAmount;
}
UpdateUI(ordersCount, totalAmount);

This code is perfectly safe, but all the benefits of using a ConcurrentDictionary are gone.

  1. The performance has become worse than using a normal Dictionary, because the internal locking inside the ConcurrentDictionary is now wasteful and redundant.
  2. You must review all your code for unprotected uses of the shared variables.
  3. You are stuck with using an awkward API (TryAdd?, AddOrUpdate?).

So my advice is: start with a Dictionary+lock, and keep the option of upgrading later to a ConcurrentDictionary as a performance optimization, if this option is actually viable. In many cases it will not be.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top