Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is deadlock possible when locking one global object in ASP.NET MVC application?

For locking I am using a single static object which is global to my application:

public class MvcApplication : System.Web.HttpApplication
{        
    public static readonly object AppLock = new object();
    ...
}

Using it for locking in code:

lock(MvcApplication.AppLock)
{
    ...
}

Let us not consider performance impact for a moment. Can I be 100% sure that I will avoid deadlock in this case?

like image 732
Evgenii Avatar asked Dec 16 '22 07:12

Evgenii


1 Answers

You can not create a deadlock conditon just with one lock-object(AppLock) See http://en.wikipedia.org/wiki/Deadlock . But it is possible with this kind of codes in threads

lock(A)
   lock(B)
       DoSomething();


lock(B)
   lock(A)
       DoSomething();
like image 197
L.B Avatar answered Jan 11 '23 22:01

L.B