views:

373

answers:

4

or should you always create some other lock object . .

+4  A: 

Yes, cast it to an IDictionary and lock on .SyncRoot:

Generic.Dictionary<int, int> dic = new Generic.Dictionary<int, int>();

lock (((IDictionary)dic).SyncRoot)
{
    // code
}

Thanks to this source for the info.

Of course a thread-safe dictionary would be nice, too, as others have suggested.

Michael Haren
+1  A: 

Hi,

You should take a look at the Thread Safe Dictionary Implementation made by Brian Rudolph which is nativly Thread Safe.

http://devplanet.com/blogs/brianr/archive/2008/09/26/thread-safe-dictionary-in-net.aspx

Yoann. B
+1  A: 

You can lock on any object that you wish (except value-types). It's recommended though to lock on the .SyncRoot object.

Vilx-
A: 

Check this question.

Also check this question:

What’s the best way of implementing a thread-safe Dictionary in .NET?

CMS