0

I have a class which holds a list of data's (Eg: List), there are methods available in classes

  1. to update the list
  2. insert new items to list and
  3. delete any items from the list

the above insert, update and delete methods are being called from multiple threads. So i have to provide lock as the following Object locker = new Object();

// Insert method
lock(locker)
{
   // Insert to list
}
// Update method
lock(locker)
{
   // Update the list
}

Now my question is which kind of locking method is good, whether to use a lock object as above or use the "syncroot" method of locking the list as below. Please advice.

// Insert method
lock(((ICollection)myList).SynRoot)
{
   // Insert to list
}
// Update method
lock(((ICollection)myList).SynRoot)
{
   // Update the list
}    

Thanks

animaonline
  • 3,715
  • 5
  • 30
  • 57
Anish
  • 872
  • 5
  • 21
  • 42

1 Answers1

0

i would suggest do some re factoring and

try looking into Blocking Collection instead of using lock explicitly if it suites with your requirement

TalentTuner
  • 17,262
  • 5
  • 38
  • 63