why no dead lock for this one?

  • Thread starter Thread starter DAXU
  • Start date Start date
D

DAXU

Hello,
I did this as a test code:

public class test{
.....
.....
public void test(int i)

{
lock(this)
{
if (i>10)
{
i--;
test(i);
}
}
}
}

I thought that when I run test(11), it should give me a deadlock as
first it asks to lock the instance of the class, then as i is bigger
than 10, it will go deeper and try to lock the instance of the class
again, as it has been locked, then it should just wait there for ever.

But it went through without any deadlock, can someone tell me what is
wrong with my understanding?

Many Thanks
Jerry
 
lock ensures that one thread does not enter a critical section of code
while another thread is in the critical section.
I should create another thread to test it, so not a valid test anyway.
 
See spec 8.12 which reads in part (edited for clarity):
lock ensures that one thread does not enter a critical section while another
thread is in the critical section of code.

....

While a mutual-exclusion lock is held, code executing in the same execution
thread can also obtain and release the lock. However, code executing in
other threads is blocked from obtaining the lock until the lock is released.


--
Regards,
Alvin Bruney
------------------------------------------------------
Shameless author plug
Excel Services for .NET is coming...
https://www.microsoft.com/MSPress/books/10933.aspx
OWC Black Book www.lulu.com/owc
Professional VSTO 2005 - Wrox/Wiley
 
Back
Top