views:

509

answers:

5

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

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

Hi, whats is the easiest way to make C# dictionary access thread safe? Preferablly just using lock(object) but any other ideas welcome!

A: 

Why don't you just use ConcurrentDictionary class?

Giorgi
Note: This class was new in .NET 4.
Mark Byers
It is only supported from .Net 4.0 doesn't it?
Itay
A: 

It is better to not lock two readers. Look here How you can use ReaderWriterLock

Itay
+1  A: 

In .NET 4 you have the ConcurrentDictionary class.

If you need to use an older version of .NET, and want to write it yourself:

  • wrap a Dictionary as a private field in your class
  • use a separate object lockObject
  • take a lock on lockObject before any access to the dictionary
Henk Holterman
+1  A: 

It was already discussed : HERE

Incognito
A: 

Thanks for the ConcurrentDictionary class, i did not know about this in .NET 4.0

Thank you

Tom