When accessing critical section in C# with async/await keywords, using lock keyword or Monitor is generally not advised (Why can't I use the 'await' operator within the body of a lock statement?), because it introduces deadlock in the situation where thread that locked critical section section will possibly be not avaliable.
The general approach is to use SemaphoreSlim
class.
My question is that since SemaphoreSlim(1,1)
and lock are functionally the same, what is the reason that it's okay to use one, but the other introduces deadlock?
Is it because SemaphoreSlim can be released from any thread, while lock is "owned" exclusively by the thread that locked it?