A
Anders Borum
Hello!
I'm am currently working on making a central cache component threadsafe, and
was looking at the synchronized implementation of the Hashtable. I was
wondering why you'd really want to enforce thread safety in a remove method?
Why would you care if two threads are trying to remove the same key from the
hashtable? In the end, the key is removed (and that was probably what you
were looking for).
Maybe this is caused by me not reading the following code correctly - and
not seing what it actually does is to make sure that no two threads are
removing keys, while other operations are performed on the hashtable (by
other threads).
public override void Remove(object key)
{
object local;
Monitor.Enter(local = _table.get_SyncRoot());
try
{
_table.Remove(key);
}
finally
{
Monitor.Exit(local);
}
}
I'm am currently working on making a central cache component threadsafe, and
was looking at the synchronized implementation of the Hashtable. I was
wondering why you'd really want to enforce thread safety in a remove method?
Why would you care if two threads are trying to remove the same key from the
hashtable? In the end, the key is removed (and that was probably what you
were looking for).
Maybe this is caused by me not reading the following code correctly - and
not seing what it actually does is to make sure that no two threads are
removing keys, while other operations are performed on the hashtable (by
other threads).
public override void Remove(object key)
{
object local;
Monitor.Enter(local = _table.get_SyncRoot());
try
{
_table.Remove(key);
}
finally
{
Monitor.Exit(local);
}
}