-1

we all know below code is used to form a critical section.

 public class CommonResource
{
    public object obj = new object();

    public void PopularFunction()
    {
        lock (obj)
        {
            ///Access variable that we want to protect form being accessed concurrently 
            ///This forms critical section
            ///My question is what is role'obj' plays in forming critical section.
            ///How it works behind the scene.
        }

        ///Above code can be written as 

        Monitor.Enter(obj);
        ///Access variable that we want to protect form being accessed concurrently 
        ///This forms critical section
        ///My question is what is role'obj' plays in forming critical section.
        ///How it works behind the scene.
        Monitor.Exit(obj);
    }
}

My question is how does Monitor.Enter forms a critical section with the help of 'obj'. If we need to always pass an object why cant framework explicitly pass any object. Definitely there has to be some reason behind this. Can anybody explain?

Thanks, Hemant

user2243747
  • 2,767
  • 6
  • 41
  • 61
  • your code is no good example for using lock or Monitor class. Creating multiple instances of the class will break your thread safty. `obj` should be static – Jehof Apr 08 '13 at 12:56
  • 1
    There's an excellent free ebook about threading here: [`Albahari`](http://www.albahari.com/threading/) – Nick Butler Apr 08 '13 at 12:56
  • read the great resource Nicholas mentioned and note that your idea of the `Monitor` equivalent of `lock` is not very precise: http://www.albahari.com/threading/part2.aspx#_MonitorEnter_and_MonitorExit – Cristian Lupascu Apr 08 '13 at 12:58
  • @Jehof: That depends entirely on what he's protecting. There's nothing that says sync objects need to be static. If what he needs to protect against is *multi-threaded access to the same instance*, then this is how you'd do it. – Adam Robinson Apr 08 '13 at 12:58
  • @AdamRobinson Of course you are right. He refers to a critical section, so i thought he wants that only one thing could access the critical section. But it depends on what he wants to do in the critical section and how the class is implemented – Jehof Apr 08 '13 at 13:03

2 Answers2

2

You're passing an object to use as an identifier for the lock. Consider I had the following class:

public class LockTest
{
    private object obj1 = new object();
    private object obj2 = new object();

    public void Method1()
    {
        lock(obj1)
        {
            ...
        }
    }

    public void Method2()
    {
        lock(obj2)
        {
            ...
        }
    }

    public void Method3()
    {
        lock(obj1)
        {
            ...
        }
    }
}

If I were to call Method1 and Method2 from different threads, neither call would block the other, since they're locking on different objects. If, however, I were to call Method1 and Method3 from different threads, the first thread to execute the lock(obj1) would block execution of the other thread until the lock was released at the end of the block.

Cristian Lupascu
  • 39,078
  • 16
  • 100
  • 137
Adam Robinson
  • 182,639
  • 35
  • 285
  • 343
1

It is used so the framework knows the scope of the lock.

Basically, you want to either use a static object or a non-static object.

public class Foo
{
    private object sync = new object();
    public void Bar()
    {
        lock (this.sync)
        {
            // You can call new Foo().Bar() multiple times, because
            // each Foo class lock its own instance of the sync object
        }
    }
}

public class Foo
{
    private static object sync = new object();
    public void Bar()
    {
        lock (sync)
        {
            // You can't call new Foo().Bar() multiple times, because
            // each Foo class lock the same instance of the sync object
        }
    }
}
ken2k
  • 48,145
  • 10
  • 116
  • 176