Which of the following two code snippets is better to use?
static readonly object _locker = new object();
lock (_locker)
or
Because you don't want the lock to be accessed from outside the object.
If you use lock(this) you can get a deadlock:
void blah() {
lock(this);
sleep(200);
}
//Some other block of code
MyObject a;
foreach(Mythread m in threads)
{
lock(a);
m.Call(a.blah); //Not the best syntax, but you get the idea.
}
If you keep the lock inside the object it wouldn't deadlock.