Does anyone know of an implementation of a lock free hash table in C#? Or can anyone confirm for a fact that at least reads on a HashTable are thread-safe?
Edit:
I can read the documentation, but it's unclear.
"It is thread safe for multi-thread use when only one of the threads perform write (update) operations."
So, the question is, if I have multiple threads, and they all could write to the hashtable, I would use the writerlock. However, those same threads also read from the hashtable. Do I need a readerlock on the reads?
Reads are thread-safe until the collection is modified.
From the documentation:
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. To support multiple writers all operations on the Hashtable must be done through the wrapper returned by the Synchronized method, provided that there are no threads reading the Hashtable object.
Enumerating through a collection is intrinsically not a thread safe procedure. Even when a collection is synchronized, other threads can still modify the collection, which causes the enumerator to throw an exception. To guarantee thread safety during enumeration, you can either lock the collection during the entire enumeration or catch the exceptions resulting from changes made by other threads.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With