I just want to make sure I understand SyncLock correctly. Given the following code is it safe to say that none of these blocks will ever be executed at the same time? Or is it safe to say that no other code will be executed during the execution of any of those blocks? I am having trouble understanding what exactly a SyncLock does and why it needs a random object to lock on?
Dim PADLOCK As Object = New Object()
Block A:
SyncLock PADLOCK
'...Code
End SyncLock
Block B:
SyncLock PADLOCK
'...Code
End SyncLock
Block C:
SyncLock PADLOCK
'...Code
End SyncLock
Not entirely.
Firstly, locks are re-entrant, so if one method calls into another (or into the same method recursively), you could have both in use by the same thread.
Secondly, if “…Code” contains a
Monitor.Wait– that relinquishes the lock; in which case you could have two threads in the methods, but only one of the threads active. TheWaitcall blocks until the waiting thread can re-obtain the lock – so it remains the case that at most one thread “has” the lock.