I wouldn't be suprised if it was someone who saw this:
private readonly object lockObj = new object();
private void MyMethod()
{
lock(lockObj)
{
// do amazing stuff, so amazing it can only run once at a time
// e.g. comands on the Mars Rover, or programs on iOS pre 4 / 5 ??
}
}
and thought he could cut the number of lines.
I'd be very worried if that were the case though...
Here is similar question, and answer:
Locks ensure mutual exclusion -- no more than one thread may hold the lock at the same time. The lock is identified with a specific object instance. You are creating a new object to lock on every time and you don't have any way to inform any other thread to lock on that exact same object instance. Therefore, your locking is useless.
It is probably useless. But there is the off chance it is there to create a memory barrier. Not sure if c# does lock elision or if it does whether it preserves the ordering semantics of the lock.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With