36

When implementing a lock, I used to create a private object inside of my class:

If I want to be sure that it is locked in the thread that created my class:

private object Locker = new object();

If I want to be sure that it will be locked for all threads inside my application:

private static object Locker = new object();

But here: Why does the lock object have to be static?

and in a number of other questions, everyone says that the object has to be readonly. I haven't found the reason - not even in MSDN or JavaDoc.

As I use this kind of construction quite often, could someone explain to me why should I use readonly?

Thanks!

1
  • There's no need for Locker (either static or not) to be readonly; but the code looks more accurate if Locker is readonly: we stress out that Locker instance can't be changed Commented Feb 27, 2014 at 8:49

4 Answers 4

76

If I want to be sure that it will be locked for all threads inside my application:

The lock object has to be static, if it locks access to static state.
Otherwise it has to be instance, because there's no need to lock state of one class instance, and prevent other threads to work with another class instance at the same time.

everyone says that the object has to be "readonly" I didn't found the reason

Well, it doesn't have to be. This is just a best practice, which helps you to avoid errors.

Consider this code:

class MyClass
{
    private object myLock = new object();
    private int state;

    public void Method1()
    {
        lock (myLock)
        {
            state = // ...
        }
    }

    public void Method2()
    {
        myLock = new object();
        lock (myLock)
        {
            state = // ...
        }
    }
}

Here Thread1 can acquire lock via Method1, but Thread2, which is going to execute Method2, will ignore this lock, because lock object was changed => the state can be corrupted.

6
  • I see, thank you very much for the explanation. Probably because I'm used to create different locks depending on the resource (or method) that I want to access, I never got in this situation, but now I understand that in any case the readonly is a good practice that won't harm my code.
    – Rafa
    Commented Feb 27, 2014 at 8:55
  • @Dennis, readonly make sense. But static is not required in lock. suppose i will not mark readonly if use static only , behaviour will be same as private object myLock = new object();
    – Sudhir.net
    Commented Nov 27, 2018 at 11:31
  • @Sudhir.net: I'm afraid, I didn't understand your point. static is required when myLock protects static state (that is, static fields). If you omit static in this case, each type instance will have its own myLock, and this makes no sense.
    – Dennis
    Commented Nov 27, 2018 at 12:14
  • Thanks Dennis. myLock vairable is private to any specific class. It will not shared among other classes. If i will use readonly then nobody can create the object. So state will be not changed then inside one class state of myLock will not be changed. Then my question is what is need of need of Static with myLock ?
    – Sudhir.net
    Commented Nov 28, 2018 at 5:51
  • 1
    @Sudhir.net: You need static if code inside lock (myLock) { ... } accesses static fields, because static fields are shared among class instances. If code inside lock works only with instance fields (that is, fields, declared without static keyword), don't use static for myLock.
    – Dennis
    Commented Nov 28, 2018 at 9:16
21

It doesn't have to be readonly, but it's good practice to as it saves you from accidentally replacing it, which can lead to some difficult to track down bugs.

12

I guess what meant is "a variable referencing a lock object should be read only".

You lock on a lock object that variable references, not on a variable itself. I.e. having

private object Locker = new object();

you lock on that new object(), not on Locker field. Then, if you replace the value of the field with a reference to another object, say

Locker = new object();

and lock on it, you are locking on two different objects, and that defeats the purpose of the locking, because you do not get a synchronized access now.

7

The object should ideally be readonly so that it cannot be changed to point to another object. If you took a lock on an object, then another thread changes the object, if, then another thread comes along, and tries to take a lock on this object, the object in question would not be the same, so the original lock would be invalidated.

This would be a very rare case. But if you had a lock on the object, and another thread waiting on that object, if the locking thread calls Monitor.Pulse, the waiting thread is awoken, and can take the lock on the object. In that period of time between being woken and taking the lock, another thread could change the object that is referenced in the lock, and so the waiting thread would take a lock on a different object.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.