Alex Rivera | Logout

How deep does a lock go?

Asked 2009-08-11T07:31:15.763
10

I have the following code:

            locker = new object();
        lock (locker)
        {
            for (int i = 0; i < 3; i++)
                 ver_store[i] = atomic_Poll(power);                
        }

I was just wandering, considering the function within the lock accesses some global resources, (an open socket among them) whether all global resources within the object are also locked. ( I am aware that any other function that accesses these same variables must implement a lock on them also for the locking mechanism to be valid. I just haven't gotten round to locking them yet :) )

Edit
Report

1 Answer

14

The lock statement does NOT "lock code" or any resource that goes in between the curly braces pre se.

I find it best to understand lock from a thread perspective (after all, threading scenarios is when you need to consider locking).

Given your sample code

10  locker = new object();
11  lock (locker)
12  {
     ...
15  }

When thread X reaches line 10 a new object is created and at line 11 a lock is acquired on the object. Thread X continues to execute whatever code is within the block.

Now, while thread X is in the middle of our block, thread Y reaches line 10. Lo and behold, a new object is created, and since it is created by thread Y no lock is currently acquired on this object. So when thread Y reaches 11 it will successfully acquire a lock on the object and continues to execute the block concurrently with thread X.

This is the situation the lock was suppose to prevent. So what to do? Make locker a shared object.

01  static object locker = new object();

    ...

11  lock (locker)
12  {
     ...
15  }

Now, when thread X reaches line 11 it will acquire the lock and start executing the block. When thread Y reaches line 11 it will try to acquire a lock on the same object as thread X. Since this object is already locked, thread Y will wait until the lock is released. Thus preventing concurrent execution of the code block, thus protecting any resources used by that code to be accessed concurrently.

Note: if other parts of your system should be serialized around the same resources they must all try to lock the same shared locker object.

answered 2009-08-11T12:49:47.403

Your Answer