Tamara,
The first option, adding the volatile keyword to the declaration will do
nothing for actually making the operations on the dictionary thread-safe.
It will only affect accessing the reference.
Your best option is to not touch anything. The performance of the
hashtable compared to the Dictionary<string, stringisn't going to be that
far off (not counting synchronization), since you are using reference types
for the key and the value, there is no boxing, and less of a performance
hit.
If you insist on using a Dictionary<string, string>, then I would create
an implementation of IDictionary<TKey, TValuewhich synchronizes calls made
to it internally.
Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
-
mvp@spam.guard.caspershouse.com
<tamara.roberson@gmail.comwrote in message
news:1158070399.005031.151660@e63g2000cwd.googlegr oups.com...
Quote:
>I am porting some code to use generics. Currently, we have a
synchronized Hashtable called as follows:
>
private Hashtable table = Hashtable.Synchronized (new Hashtable ());
>
What I would like to do is to port this to a Dictionary so that it can
be type-safe:
>
private Dictionary<string,stringtable = new Dictionary<string,string>
();
>
But this loses our synchronization. I was suprised to find that there
is no built-in method for an automatically synchronized Dictionary. So
what should I do?
>
Some options:
>
* add 'volatile' keyword to declaration
* wrap all modifications of the table with lock(table) { ... }
* use some other locking mechanism
* stick to using Hashtable for now, threading hurts my my head and its
better to just leave it alone and not break the whole thing :-)
>
Thanks!
Tamara
>