Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Lock free hash table for c#

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?

like image 316
jvenema Avatar asked Jun 11 '09 18:06

jvenema


2 Answers

Reads are thread-safe until the collection is modified.

like image 164
Orion Adrian Avatar answered Oct 21 '22 16:10

Orion Adrian


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.

like image 25
Kekoa Avatar answered Oct 21 '22 17:10

Kekoa