I was recently in an interview and the tech guy asked me about how to make an application thread-safe.
Well, after explaining the lock()
correctly, he s
Use a non static object for the lock whenever you need to make sure the same instance isn't manipulated by different threads at the same time.
Lets say you have some List classes, with a special Reorder method that accepts some strange arguments. Consider if you need to reorder 100 different lists during some paralel processes. You only care that different threads don't manipulate the same list at the same time, as it might affect your reorder logic. You don't need a static lock, as you don't care when different lists are being manipulated at the same time.
A simple example of a scenario with a static lock, is initialization of some static data, where you want to make sure the load logic is ran only once. Like some Cache or a Singleton.