I wrote a simple code (attached), and I do not understand why the lock on some block does not block the area.
The code:
object locker = new object(); private void foo(int i) { Console.WriteLine( string.Format( "i is {0}", i ) ); lock( locker ) { while( true ) { Console.WriteLine( string.Format( "i in while loop is {0}", i ) ) ; foo( ++i ); } } }
I expect that calling the foo method in a while loop will wait for the lock to be released (the locker area), but all calls to foo with arg of ++ I can go into the blocking block.
Yanshof
source share