6

In terms of thread-safety, is there any difference between HashTable and Dictionary? I don't see any...According to msdn, both are defined as follows :-

Hashtable

Hashtable is thread safe for use by multiple reader threads and a single writing thread. It is thread safe for multi-thread use when only one of the threads perform write (update) operations, which allows for lock-free reads provided that the writers are serialized to the Hashtable.

Dictionary

A Dictionary can support multiple readers concurrently, as long as the collection is not modified. Even so, enumerating through a collection is intrinsically not a thread-safe procedure. In the rare case where an enumeration contends with write accesses, the collection must be locked during the entire enumeration. To allow the collection to be accessed by multiple threads for reading and writing, you must implement your own synchronization.

Cameron Skinner
  • 51,692
  • 2
  • 65
  • 86
rajibdotnet
  • 1,498
  • 2
  • 17
  • 29
  • 3
    HashTable = not generic, don’t use it. Ever. Deprecated. – Konrad Rudolph Apr 21 '12 at 01:43
  • @KonradRudolph, it's not deprecated, but IMO it should be... – Thomas Levesque Apr 21 '12 at 01:46
  • @Thomas Exactly. I declare it so. I have no idea why Microsoft still hasn’t pulled the plug on these classes but you don’t use them in polite company. – Konrad Rudolph Apr 21 '12 at 01:48
  • My question was limited only about thread-safety which is bugging me constantly. Regarding Generics part, I am aware of it. – rajibdotnet Apr 21 '12 at 01:50
  • Dictionary is not just generic replacement for Hashtable, they both targets on different scenarios. Dictionary is optimized for maximum performance in single-thread, but Hashtable is better for multiple readers and writers(s) and also has many methods declared as virtual thus allows overriding Add/Remove/Contains, etc. – Ňuf Apr 21 '12 at 02:09
  • Perhaps Microsoft hasn't pulled those classes because there is old code that still uses them. There's no particular reason they should be removed, other than the non-generic collections seem to offend some people's sensibilities. – Jim Mischel Apr 21 '12 at 04:39
  • Saying Hashtable should never be used is wrong. Unlike Dictionary, Hashtable allows multiple lock-free readers as long as writing is synchronized. For applications that have lots of readers and can synchronize writing, Hashtable significantly outperforms Dictionary. – Anton Jan 11 '16 at 21:02
  • At the end, after more than 20 years, for low level libraries I still use Hashtables. Sometimes for lock, sometimes for null return instead of exception, sometimes I don't know why. In low level libraries I still use Hashtables... – bubi Apr 08 '21 at 09:13

1 Answers1

14

Both classes allows multiple readers at once without lock, both must be locked for multiple writers. The difference is, that Hashtable allows ONE writer together with multiple readers without locking, while this is not safe with Dictionary. So with Hashtable only writes must be locked. If both Keys and Values are reference-type (and so no boxing/unboxing is needed), Hashtable can be faster than Dictionary in scenarios with many readers and one (or more) writers, because readers don't have to wait for lock at all. With Dictionary the same scenario requires using ReaderWriterLock.

user961954
  • 3,214
  • 2
  • 17
  • 24
Ňuf
  • 6,027
  • 2
  • 23
  • 26