I have a class which holds a list of data's (Eg: List), there are methods available in classes
- to update the list
- insert new items to list and
- 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