views:

368

answers:

2

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.

+1  A: 

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
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<TKey, TValue>` (http://msdn.microsoft.com/en-us/library/dd287191(VS.100).aspx)?
Jason
Surely you mean ConcurrentDictionary? http://msdn.microsoft.com/en-us/library/dd287191(VS.100).aspx
Michael Greene
@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
@JaredPar: Dude, no coffee and no food make programmer go something something.
Jason
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?
Sam
@Sam, I added a link with a bit more info
JaredPar
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."
Sam
A: 

In http://stackoverflow.com/questions/157933/whats-the-best-way-of-implementing-a-thread-safe-dictionary-in-net 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.

ebpower
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.aspxSynchronized Hashtable is exactly what I was trying to avoid, simple lock() on all access.
Sam
Will you be using that or wait for the .Net 4 ConcurrentDictionary?
ebpower