C# Lock and Async Method

后端 未结 4 1817
星月不相逢
星月不相逢 2021-02-05 03:22

I am not clear (and can\'t find documentation clear enough): when using the lock keyword in an async method: will the thread be blocked if the object is already blocked or will

4条回答
  •  独厮守ぢ
    2021-02-05 03:46

    You task will not return in suspended state. It will wait until myLock is unlocked to run the code within lock statement. It will happen no matter what C# asynchronous model you use.

    In other words, no two threads will be able to run statements inside the lock. Unless, there are many different instances of myLock object.

提交回复
热议问题