M
Mark
When the MSDN docs say
"Hashtable is thread safe for use by multiple reader threads and a single
writing thread ... which allows for lock-free reads provided that the writers
are serialized to the Hashtable"
how is that accomplished? If the readers are flipping through the table,
even though the operation is quick, don't they need to lock it if there's a
writer out there?
Also, if the hash table in question is a member of a larger class and the
"writer" may swap in a whole new table periodically and the readers are all
"lock-free" I presume that using Interlocked.Exchange<Hashtable>() would be
sufficient to make sure the readers don't hit a pothole?
Also also, it seems like the change in behavior between Hashtable and
generics would complicate concurrency. Hashtable has the behavior that
lookups that aren't found return null; Dictionary<> throws an error,
necessitating a 2-step lookup.
e.g.
object foo = myClass.Hashtable["foo"];
if (foo == null) Console.Writeline("Not found");
vs
object foo = myClass.Dictionary["foo"]; // blows up if not found
or
if (myClass.Dictionary.ContainsKey("foo")) foo = myClass.Dictionary["foo"];
From the external point of view, lookup is an atomic operation for Hashtable
but not for Dictionary. If someone swaps out the Hashtable with an
interlocked exchange, the Hashtable reader will still work with the one in
hand. If someone swaps out a Dictionary, you could catch a reader between
the ContainsKey and the lookup.
Thanks
Mark
"Hashtable is thread safe for use by multiple reader threads and a single
writing thread ... which allows for lock-free reads provided that the writers
are serialized to the Hashtable"
how is that accomplished? If the readers are flipping through the table,
even though the operation is quick, don't they need to lock it if there's a
writer out there?
Also, if the hash table in question is a member of a larger class and the
"writer" may swap in a whole new table periodically and the readers are all
"lock-free" I presume that using Interlocked.Exchange<Hashtable>() would be
sufficient to make sure the readers don't hit a pothole?
Also also, it seems like the change in behavior between Hashtable and
generics would complicate concurrency. Hashtable has the behavior that
lookups that aren't found return null; Dictionary<> throws an error,
necessitating a 2-step lookup.
e.g.
object foo = myClass.Hashtable["foo"];
if (foo == null) Console.Writeline("Not found");
vs
object foo = myClass.Dictionary["foo"]; // blows up if not found
or
if (myClass.Dictionary.ContainsKey("foo")) foo = myClass.Dictionary["foo"];
From the external point of view, lookup is an atomic operation for Hashtable
but not for Dictionary. If someone swaps out the Hashtable with an
interlocked exchange, the Hashtable reader will still work with the one in
hand. If someone swaps out a Dictionary, you could catch a reader between
the ContainsKey and the lookup.
Thanks
Mark