I have a List of objects shared by multiple threads, it sometimes generate IndexOutOfRangeException when trying to Clear
. While searching for solution I found that I should use SyncLock while accessing the List.
But my question if what is the importance of lockObject
in SyncLock
block
e.g. while clearing myList
can I use
Synclock myList
myList.Clear
End SyncLock
or lockObject should be different from myList?
Edit:
What I think about sysnclock is "lock is obtained for object specified as lockObject". What if I specify list to be cleared as lockObject, shouldn't the compiler supposed to obtain the exclusive access to list before clearing it ?