468,107 Members | 1,365 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,107 developers. It's quick & easy.

Thread-safe Dictionary

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

Sep 12 '06 #1
2 26112
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]
- mv*@spam.guard.caspershouse.com

<ta*************@gmail.comwrote in message
news:11**********************@e63g2000cwd.googlegr oups.com...
>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

Sep 12 '06 #2
Here is a synced version below. I have to admit I did not test this fully.
Nor am I totally convinced that some of these methods really don't need a
lock (i.e. ContainsKey, Count, etc) - but I used SyncHashTable as a guide
there. To be totally sure, you could add the lock.

public class SafeDictionary<TKey, TValue: IDictionary<TKey, TValue>
{
private readonly object syncRoot = new object();
private Dictionary<TKey, TValued = new Dictionary<TKey, TValue>();

#region IDictionary<TKey,TValueMembers

public void Add(TKey key, TValue value)
{
lock (syncRoot)
{
d.Add(key, value);
}
}

public bool ContainsKey(TKey key)
{
return d.ContainsKey(key);
}

public ICollection<TKeyKeys
{
get
{
lock (syncRoot)
{
return d.Keys;
}
}
}

public bool Remove(TKey key)
{
lock (syncRoot)
{
return d.Remove(key);
}
}

public bool TryGetValue(TKey key, out TValue value)
{
lock (syncRoot)
{
return d.TryGetValue(key, out value);
}
}

public ICollection<TValueValues
{
get
{
lock (syncRoot)
{
return d.Values;
}
}
}

public TValue this[TKey key]
{
get
{
return d[key];
}
set
{
lock (syncRoot)
{
d[key] = value;
}
}
}

#endregion

#region ICollection<KeyValuePair<TKey,TValue>Members

public void Add(KeyValuePair<TKey, TValueitem)
{
lock (syncRoot)
{
((ICollection<KeyValuePair<TKey, TValue>>)d).Add(item);
}
}

public void Clear()
{
lock (syncRoot)
{
d.Clear();
}
}

public bool Contains(KeyValuePair<TKey, TValueitem)
{
return ((ICollection<KeyValuePair<TKey,
TValue>>)d).Contains(item);
}

public void CopyTo(KeyValuePair<TKey, TValue>[] array, int
arrayIndex)
{
lock (syncRoot)
{
((ICollection<KeyValuePair<TKey, TValue>>)d).CopyTo(array,
arrayIndex);
}
}

public int Count
{
get
{
return d.Count;
}
}

public bool IsReadOnly
{
get { return false; }
}

public bool Remove(KeyValuePair<TKey, TValueitem)
{
lock (syncRoot)
{
return ((ICollection<KeyValuePair<TKey,
TValue>>)d).Remove(item);
}
}

#endregion

#region IEnumerable<KeyValuePair<TKey,TValue>Members

public IEnumerator<KeyValuePair<TKey, TValue>GetEnumerator()
{
return ((ICollection<KeyValuePair<TKey,
TValue>>)d).GetEnumerator();
}

#endregion

#region IEnumerable Members

System.Collections.IEnumerator
System.Collections.IEnumerable.GetEnumerator()
{
return ((System.Collections.IEnumerable)d).GetEnumerator( );
}

#endregion
}

--
William Stacey [MVP]

<ta*************@gmail.comwrote in message
news:11**********************@e63g2000cwd.googlegr oups.com...
|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
|
Sep 12 '06 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

14 posts views Thread by adeger | last post: by
4 posts views Thread by Gilles Leblanc | last post: by
7 posts views Thread by Ivan | last post: by
4 posts views Thread by Matthew Groch | last post: by
5 posts views Thread by Razzie | last post: by
16 posts views Thread by droopytoon | last post: by
9 posts views Thread by mareal | last post: by
13 posts views Thread by Bob Day | last post: by
7 posts views Thread by Charles Law | last post: by
3 posts views Thread by John Nagle | last post: by
1 post views Thread by Solo | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.