6

Which is preferred in a multi-threaded application: Dictionary with lock object or Concurrency Dictionary

Which is efficient and why should I use one or the other?

edit 1: Storing Guid as key and bool as value.

edit 2: more than 2 worker threads and one UI thread.

CodesInChaos
  • 106,488
  • 23
  • 218
  • 262
radu florescu
  • 4,315
  • 10
  • 60
  • 92
  • 2
    It will depend very much on the situation. – H H Mar 26 '12 at 07:44
  • 1
    Possible duplicate of [dictionary-locking-vs-concurrentdictionary](http://stackoverflow.com/questions/1949131/net-dictionary-locking-vs-concurrentdictionary) – Michael Freidgeim May 03 '13 at 18:46
  • Possible duplicate of [.NET - Dictionary locking vs. ConcurrentDictionary](https://stackoverflow.com/questions/1949131/net-dictionary-locking-vs-concurrentdictionary) – Sinatr Sep 12 '19 at 09:55

4 Answers4

6

I would say you've got the following options.

Some new Framework 4.0 classes:

  • ConcurrentDictionary. Work fast and reliable.
  • ConcurrentBag. It's unordered collection of objects, so it works faster, but suits if you don't need sorting only.
  • ConcurrentStack. It's an implementation of the classic LIFO (Last-In First-Out) data structure that provides thread-safe access without the need for external synchronization
  • ConcurrentQueue. It's a thread-safe FIFO (first in-first out) collection.

All new 4.0 classes work faster but have some features mentioned by levanovd. Performance comparison of these classes you can find here.

Some classic solutions from earlier versions:

  • Dictionary + Monitor. Simple wrap to lock
  • Dictionary + ReaderWriterLock. Better than the previous one, because has got read and write locks. So several threads can read, and just one - write.
  • Dictionary + ReaderWriterLockSlim. It's just optimisation of the previous one.
  • Hashtable. From my experience, it's the slowest method. Check Hashtable.Synchronized() method, it's a ready to go solution from Microsoft.

If I had a restriction of using Framework v3.5, I would use Dictionary + ReaderWriterLock or ReaderWriterLockSlim.

Alex Klaus
  • 8,168
  • 8
  • 71
  • 87
5

Read carefully about ConcurrentDictionary. It has some unobvious features.

Here are some of them:

  • If two threads call AddOrUpdate there's no guarantees about which of factory delegates will be called and even no guarantee that if a factory delegate will produce some item that this item will be stored in dictionary.
  • Enumerator obtained by GetEnumerator call is not a snapshot and may be modified during enumeration (that doesn't cause any exceptions).
  • Keys and Values properties are snapshots of corresponding collections and may not correspond to actual dictionary state.
  • etc.

So please read about ConcurrentDictionary again and decide if this behavior is what you need.

Hope this helps!

levanovd
  • 4,095
  • 6
  • 37
  • 56
4

When you are implementing a dictionary with a lock object, your main concern seems like thread safety. So it seems, a concurrentDictionary already manages this concern. I think there is no point in re-inventing the wheel.

daryal
  • 14,643
  • 4
  • 38
  • 54
  • 2
    Not always. If speed is at a premium the manual locking might be better. – H H Mar 26 '12 at 07:46
  • 1
    Depending on the situation, you may be right , on the otherhand, there is no applicable performance data on this situation. Custom testing may verify the performance gain(if any). – daryal Mar 26 '12 at 07:50
  • 1
    Then it depends what kind of objects you are storing in the dictionary, the expected length of the dictionary, the expected number of threads accesing the dictionary, implementation of custom locking, the expected ratio of concurrent acceses, etc. – daryal Mar 26 '12 at 08:15
1

I think both will provide thread-safety but using a Dictionary with lock object will limit the number of thread that can access the Dictionary concurrently to 1. While using Concurrent Dictionary, you can specify concurrent level (i.e. number of threads that can access the Dictionary concurrently). If performance does matter, I believe Concurrent Dictionary should be your choice.

Thang Mai
  • 314
  • 3
  • 10