Does a locked object stay locked if an exception occurs inside it?

后端 未结 6 1102
长情又很酷
长情又很酷 2020-11-27 02:39

In a c# threading app, if I were to lock an object, let us say a queue, and if an exception occurs, will the object stay locked? Here is the pseudo-code:

in         


        
6条回答
  •  旧时难觅i
    2020-11-27 03:07

    "A lock statement is compiled to a call to Monitor.Enter, and then a try…finally block. In the finally block, Monitor.Exit is called.

    The JIT code generation for both x86 and x64 ensures that a thread abort cannot occur between a Monitor.Enter call and a try block that immediately follows it."

    Taken from: This site

提交回复
热议问题