6

Are there any available implementations of a Hashtable that provide thread safety with minimal locking in .NET? Or in another language that can be ported to .NET?

We're looking for something in between using a BCL Dictionary<,> class with lock() and a distributed caching application like memcached or Velocity.

The intended use is for a cache with thousands of readers reading out immutable values based on keys (either numbers or guids, we haven't decided which yet). There will be far less writers, possibly only one.

Samuel Neff
  • 73,278
  • 17
  • 138
  • 182
  • Please define what you mean by "thread-safe." cf. http://blogs.msdn.com/ericlippert/archive/2009/10/19/what-is-this-thing-you-call-thread-safe.aspx – jason Jan 21 '10 at 16:49
  • It might also be helpful to describe your usage scenario. Are you going to be mixing insert/lookup/remove or are they going to be grouped together somehow? Are all the operations going to be accessed from many threads, or only certain ones? – Dolphin Jan 21 '10 at 16:52

2 Answers2

4

Starting in .Net 4.0 there is ConcurrentDictionary. This is a hashtable style structure meant for high performance use between multiple threads.

Details on it's use and implementation can be found here:

JaredPar
  • 733,204
  • 149
  • 1,241
  • 1,454
  • That doesn't look like it's indexable by a key; i.e., there is no `O(1)` way to get a specific keyed value out. `ConcurrentBag` seems more useful for producer/consumer scenarios. Maybe you meant `ConcurrentDictionary` (http://msdn.microsoft.com/en-us/library/dd287191(VS.100).aspx)? – jason Jan 21 '10 at 16:52
  • Surely you mean ConcurrentDictionary? http://msdn.microsoft.com/en-us/library/dd287191(VS.100).aspx – Michael Greene Jan 21 '10 at 16:53
  • @Jason, @Michael thanks, yes I meant ConcurrentDictionary. My best excuse is it's early and I'm on SO before I drank my coffee. – JaredPar Jan 21 '10 at 16:55
  • The MSDN docs just say it provides a thread-safe dictionary with no details on exactly how it achieves thread-safety. Do you have any links with more details and if it's better than the old Synchronized Hashtable implementation? – Samuel Neff Jan 21 '10 at 17:57
  • @Sam, I added a link with a bit more info – JaredPar Jan 21 '10 at 18:16
  • Thanks for the extra link. The article has a very small amount of information about inner details but the author added a comment later that has a lot more details. " ConcurrentDictionary does use fine-grained locking internally, i.e. multiple locks rather than a single lock in order to minimize lock contention. The number of locks employed is actually controllable as well through the concurrencyLevel parameter available on several of the dictionary's constructors. Note, however, that this locking is done only for writes; reads on the dictionary are implemented in a lock-free manner. " – Samuel Neff Jan 21 '10 at 20:22
1

In What's the best way of implementing a thread-safe Dictionary? Brian Rudolf shares a link to a thread-safe dictionary that uses ReaderWriterLockSlim: http://devplanet.com/blogs/brianr/archive/2008/09/26/thread-safe-dictionary-in-net.aspx.

You might also look at the Synchronized Hashtable: http://msdn.microsoft.com/en-us/library/system.collections.hashtable.aspx.

Community
  • 1
  • 1
Ed Power
  • 8,310
  • 3
  • 36
  • 42
  • The ReaderWriterLockSlim based dictionary looks like exactly what I was asking for--more efficient locking. Direct link: http://devplanet.com/blogs/brianr/archive/2008/09/26/thread-safe-dictionary-in-net.aspx Synchronized Hashtable is exactly what I was trying to avoid, simple lock() on all access. – Samuel Neff Jan 21 '10 at 17:51
  • Will you be using that or wait for the .Net 4 ConcurrentDictionary? – Ed Power Jan 23 '10 at 00:13