nested lock?

This is a discussion on nested lock? within the C# Programming forums, part of the General Programming Boards category; Hello everyone, From the sample, http://www.albahari.com/threading/part2.html Do you know what means "A thread can block only on the first, or ...

  1. #1
    Registered User
    Join Date
    May 2006
    Posts
    1,579

    nested lock?

    Hello everyone,


    From the sample,

    http://www.albahari.com/threading/part2.html

    Do you know what means "A thread can block only on the first, or outermost lock."?

    I quote the related context below as well.

    --------------------
    Nested Locking
    A thread can repeatedly lock the same object, either via multiple calls to Monitor.Enter, or via nested lock statements. The object is then unlocked when a corresponding number of Monitor.Exit statements have executed, or the outermost lock statement has exited. This allows for the most natural semantics when one method calls another as follows:

    Code:
    static object x = new object();
     
    static void Main() {
      lock (x) {
         Console.WriteLine ("I have the lock");
         Nest();
         Console.WriteLine ("I still have the lock");
      }
      Here the lock is released.
    }
     
    static void Nest() {
      lock (x) {
        ... 
      }
      Released the lock? Not quite!
    }
    A thread can block only on the first, or outermost lock.
    --------------------


    thanks in advance,
    George

  2. #2
    pwns nooblars
    Join Date
    Oct 2005
    Location
    Portland, Or
    Posts
    1,094
    It means that you have to unlock as many times as you lock in order for the resource to be unlocked. That way if you lock something, lock it again, then unlock it once (because that portion of the code is done with the variable but the calling portion of the code feels the need to lock it) it is still locked and you must unlock it again.

  3. #3
    Registered User
    Join Date
    May 2006
    Posts
    1,579
    Thanks Wraithan,


    Question answered.

    Quote Originally Posted by Wraithan View Post
    It means that you have to unlock as many times as you lock in order for the resource to be unlocked. That way if you lock something, lock it again, then unlock it once (because that portion of the code is done with the variable but the calling portion of the code feels the need to lock it) it is still locked and you must unlock it again.

    regards,
    George

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. lock needed in this scenario?
    By George2 in forum C# Programming
    Replies: 1
    Last Post: 05-25-2008, 07:22 AM
  2. read write lock in C#
    By George2 in forum C# Programming
    Replies: 0
    Last Post: 04-16-2008, 08:49 AM
  3. Atomic Operations
    By Elysia in forum Windows Programming
    Replies: 27
    Last Post: 03-27-2008, 02:38 AM
  4. Smart pointer class
    By Elysia in forum C++ Programming
    Replies: 63
    Last Post: 11-03-2007, 07:05 AM
  5. deriving classes
    By l2u in forum C++ Programming
    Replies: 12
    Last Post: 01-15-2007, 04:01 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21