I have seen different variations of objects used when acquiring a lock
A static private object
public class MyClass { private static object syncBlock = new object(); private void MyMethod() { lock (syncBlock) { } }
}
A class level private object
public class MyClass { private object syncBlock = new object(); private void MyMethod() { lock (syncBlock) { } } }
using the type itself
public class MyClass { private void MyMethod() { lock (typeof (MyClass)) { } } }
using this:
public class MyClass { private void MyMethod() { lock (this) { } } }
Can someone elaborate what are the pro/cons of each of these and if one should be preferred over others in a given scenario.