only adding or fetching a reference from the dictionary is thread safe. you must make sure the item stored in the dictionary is thread and accessing the properties is thead safe. strings because they are imutable are thread safe for this use.
thanks for your reply but I dont think that answers my question.I just gave an example by using string. I may using IList. anyway the question was which of the two methods I mentioned are thread safe? Thanks
Are both method threadsafe. I mean if two processes try to access the dictionary at the same time, it will not throw an exception?
Yes they are thread safe. The ConcurrentDictionary class internally uses Monitor(C# lock). You can see
this for understanding why normal dictionaries are not thread safe.
"And whoever is removed away from the Fire and admitted to Paradise, he indeed is successful." (The Holy Quran)
Excellent Windows VPS Hosting Imran Baloch MVP, MVB, MCP, MCTS, MCPD
Marked as answer by pantonis on May 06, 2012 08:44 AM
Please look at this post as this addresses how the contents are not threadsafe in regards to AddOrUpdate() and the concurrent dictionary. Yes the concurrentdictionary is threadsafe and in most situations
you will not encounter an issue but be sure to read how threadsafe was defined.
pantonis
Member
328 Points
260 Posts
Concurrent dictionary thread safe
May 01, 2012 02:11 PM|LINK
Im using MVC3. I have a question about ConcurrentDictionary.
I have the following:
public ConcurrentDictionary<String, String> myDictionary;
There are two ways of adding data.
myDictionary["SomeKey"] = "Some value"
and
myDictionary.TryAdd("SomeKey", "Some value");
Are both method threadsafe. I mean if two processes try to access the dictionary at the same time, it will not throw an exception?
Thanks
Please mark this post as answer if it helped you solve your problem
bruce (sqlwo...
All-Star
36852 Points
5446 Posts
Re: Concurrent dictionary thread safe
May 01, 2012 03:08 PM|LINK
only adding or fetching a reference from the dictionary is thread safe. you must make sure the item stored in the dictionary is thread and accessing the properties is thead safe. strings because they are imutable are thread safe for this use.
pantonis
Member
328 Points
260 Posts
Re: Concurrent dictionary thread safe
May 01, 2012 05:42 PM|LINK
Please mark this post as answer if it helped you solve your problem
imran_ku07
All-Star
45815 Points
7698 Posts
MVP
Re: Concurrent dictionary thread safe
May 05, 2012 07:28 AM|LINK
Yes they are thread safe. The ConcurrentDictionary class internally uses Monitor(C# lock). You can see this for understanding why normal dictionaries are not thread safe.
Excellent Windows VPS Hosting
Imran Baloch MVP, MVB, MCP, MCTS, MCPD
chilberto
Member
2 Points
1 Post
Re: Concurrent dictionary thread safe
Mar 21, 2013 12:15 AM|LINK
Please look at this post as this addresses how the contents are not threadsafe in regards to AddOrUpdate() and the concurrent dictionary. Yes the concurrentdictionary is threadsafe and in most situations you will not encounter an issue but be sure to read how threadsafe was defined.