Using lock(obj) inside a recursive call

Posted by Amby on Stack Overflow See other posts from Stack Overflow or by Amby
Published on 2010-03-09T08:04:51Z Indexed on 2010/03/09 8:06 UTC
Read the original article Hit count: 257

Filed under:
|
|
|

As per my understanding a lock is not released until the runtime completes the code block of the lock(obj) ( because when the block completes it calls Monitor.Exit(obj).

With this understanding i am not able to understand the reason behind the behaviour of the following code.

private static string obj = "";
        private static void RecurseSome(int number)
        {
            Console.WriteLine(number);
            lock (obj)
            {
                RecurseSome(++number);
            }
        }

//Call: RecurseSome(0)

//Output: 0 1 2 3...... stack overflow exception

There must be some concept that i am missing. Please help.

© Stack Overflow or respective owner

Related posts about c#

Related posts about multithreading