Is a deadlock possible when locking a single global object in an ASP.NET MVC application?

For blocking, I use one static object global for my application:

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

Use to lock code:

 lock(MvcApplication.AppLock) { ... } 

Let's not look at the impact of performance on the moment. Can I be 100% sure that in this case I will avoid a dead end?

+4
source share
2 answers

You cannot create a deadlock condition with only one blocking object (AppLock). See http://en.wikipedia.org/wiki/Deadlock . But this is possible with these types of codes in streams.

 lock(A) lock(B) DoSomething(); lock(B) lock(A) DoSomething(); 
+12
source

I don't know if this is possible in ASP.NET, but in winforms / wpf you can do it.

Deadlock with one locked object?

Another deadlock scenario occurs when Dispatcher.Invoke (in a WPF application) or Control.Invoke (in a Windows Forms application) is called and is in possession of the lock. If another method works in the user interface that expects the same lock, a deadlock will occur there. This can often be fixed by simply calling the BeginInvoke call. In addition, you can release your lock before calling the Call, although this will not work if your caller has obtained a lock. We explain Invoke and BeginInvoke in Rich Client Applications and Thread Affinity.

source: http://www.albahari.com/threading/part2.aspx

0
source

Source: https://habr.com/ru/post/1380371/


All Articles