0

If in our program we are using Threads to access lets say shared collection, then we should ensure thread safety with Mutex, Monitor or Sempahore, et.c

but If we are not using Threads but we are using Tasks and then multiple tasks are trying to access common shared collection then also we should ensure safety by some methods But If we use some readymade threadsafe collection like ConcurrentDictionary then ensuring locking and thread-task safety is not required as it is already handled at framework level.

So basically i want to know which approach can be used if we are working with shared resource in concurrent consumer environment.

  • http://stackoverflow.com/questions/1949131/net-dictionary-locking-vs-concurrentdictionary – ken2k Sep 04 '12 at 08:34

2 Answers2

1

They're all great solutions for different problems. If you can tell us precisely what you're trying to do, what resources are shared, what kinds of accesses are required, then we can tell you which is probably right for your solution.

hcarver
  • 7,126
  • 4
  • 41
  • 67
  • If you want to go in detail then I am explaining some workflow like , there is a list of Quiz which can be accessible by lot of students of a class, while can be checked remarked by faculties and submitted edited by quiz creators (admins) all at the same time , suppose Quiz for Class A is running , while Quiz of Class B is being getting created and so on and also no of students in class can be 90 or so there are much such interrelated client - server request response scenario using that same collection . :( – Abhishek Gupta Sep 04 '12 at 08:40
  • If all of your only concurrency problem is accesses to that single object, then you should only make that object thread-safe, i.e. by using the `ConcurrentDictionary`. – hcarver Sep 04 '12 at 08:43
0

Overall, unless you've got very specific performance requirements, go with the easiest solution. That is, the ConcurrentDictionary. Since the synchronization logic is built-in, you can be almost certain that nobody will mess up. 'Manual' task and thread synchronization can be pretty tricky at times.

Kevin Gosse
  • 38,392
  • 3
  • 78
  • 94