multithreading - C# Lock Statement -


im going use c# lock statement , im wondering if call function while inside lock, , function lock on same object program freeze?

for example:

object object;  public void f0() {    f1(object); }  public void f1(object obj1) {    lock (obj1) {        f2(obj1);    } }  public void f2(object obj2) {   lock(obj2) {       /// operations using obj2   }  } 

here function f0 calls function f1 lock, inside lock calls f2 lock. program freeze in f2 waiting object locked in f1?

i have big program functions calling each other passing objects around, , need lock objects, may happen function call may lock same object, may locked twice, causing possible deadlock.

it may difficult know if same objects gets passed around , may end getting locked twice.

also program has multi-threading , multiple classes.

also if 1 thread gets exception inside lock, somehow unwind , fix itself?

anybody knows proper way this? have big program before start changing decide better find out proper way it.

thanks

monitor (which used lock statement under covers) reentrant, it's technically ok same thread lock on object multiple times. lock released when outer lock scope completes. however, reentrant locks difficult reason , should avoided unless have no other option.

deadlocks not occur due reentrant locks. occur when take out locks on multiple objects while other thread locks same objects in different order.


Comments

Popular posts from this blog

php - Vagrant up error - Uncaught Reflection Exception: Class DOMDocument does not exist -

vue.js - Create hooks for automated testing -

Add new key value to json node in java -