VB.NET Do I need multiple SyncLocks if I want to lock multiple things?
VB.NET 2010, .NET 4
Hello all,
My question is, imagine I h开发者_Go百科ave two List(Of T) objects and a subroutine in a multithreaded environment that modifies both of these objects. I don't understand locks very well, so I'm unsure whether I can just do:
SyncLock CType(List1, IList).SyncRoot
List1.Clear()
List2.Clear()
End SyncLock
or must I:
SyncLock CType(List1, IList).SyncRoot
SyncLock CType(List2, IList).SyncRoot
List1.Clear()
List2.Clear()
End SyncLock
End SyncLock
? Any insight? Am I even on the right track? Any comments will be appreciated.
Thanks a lot in advance, Brian
First, it is bad practice to lock on a non-private object, as something else might lock on it, and then things go downhill from there, instead lock on a private member object, something like
Class Class1
Private lockObject as New Object
Public Sub Clear()
SyncLock lockObject
...
End Synclock
End Sub
End Class
Now, for the actual question: Unless every operation you do modifies both lists (doubtful), you should have one lock per list. While possible to use one lock object to mean "I'm doing something with a list", it doesn't make much sense to block a method in another thread that isn't operating on the same list as yours, and will just slow down everything.
So, in short: Use one lock object per set of locks (Locks for operating on list1, locks for list 2, etc). It's slightly more code for sections that operate on both lists, but the code will be more performant.
Also, as a general note: Hold the lock for as little time as possible. The less time you spent in a lock, the less chance another thread will come along and be blocked until you're done.
The MSDN Page on SyncLock might also be worth a read, it contains this information and a few examples. And @BasicLife is correct, always make sure to take the locks in the same order everywhere.
And a general rule throughout the framework, unless you are accessing a static member on a class, unless otherwise stated, it is not thread safe. So when operating on lists, you will want to lock when you add, remove, clear or enumerate over the list, I'm sure there's others, but those are the most common.
Just to expand on Matt's answer, make sure you lock objects in the same order if you need multiple locks
If in Method1
you do:
Lock List 1
Lock List 2
Release 2
Release 1
and in Method2
you do:
Lock List 2
Lock List 1
Release 1
Release 2
Then you can get a deadlock situation where if both methods are called at the same time by different threads, you can get this:
thread 1 on Method1 locks list 1
thread 2 on Method2 locks list 2
thread 1 on Method1 waits until list 2 is released
thread 2 on Method2 waits until list 1 is released
Threads 1 and 2 are now deadlocked and eventually one or both will be killed off
精彩评论