c# - Attempting to acquire a lock on an object when the calling method already has a lock on the same object -
i have code have been going on larn scheme , ran across code me code smell , wouldn't think work @ all, does.
we have 2 objects, object a , object b. object a contains lock object:
private object lockobj = new object(); object b grab lock on object a.lockobj , while b has lock calls
a.somemethod(); a.somemethod() acquires lock on
this.lockobj and show in code:
threadtestone:
public class threadtestone { public object lockobject = new object(); private list<string> lst; private threadtesttwo two; public list<string> lst { { homecoming this.lst; } set { this.lst = value; } } public void run() { lst = new list<string>(); 2 = new threadtesttwo(); two.run(this); } public void end() { console.writeline("threadtestone.end"); two.end(); } public void lockme() { console.writeline("threadtestone.lockme"); lock (this.lockobject) lst.add("something"); thread.sleep(500); } } threadtesttwo:
public class threadtesttwo { private threadtestone one; private thread mythread; private bool ending = false; public void run(threadtestone a) { 1 = a; mythread = new thread(new threadstart(consume)); console.writeline("threadtesttwo starting thread"); mythread.start(); } public void end() { console.writeline("threadtesttwo.end"); ending = true; mythread.join(); } public void consume() { while (!ending) { console.writeline("threadtesttwo one.lockobject"); lock (one.lockobject) { console.writeline("two.lockme"); one.lockme(); one.lst.add("two"); thread.sleep(500); } } } } when on above code, think should break one.lockme() should never able acquire lock on lockobj because threadtesttwo has lock.
i thought result in deadlock. however, when run above illustration code, works. also, code reviewing works , in production.
the fact doesn't result in exception beingness thrown confusing me. wrong in assuming should error?
in code testing reading info after trying acquire lock twice had thought compiler removing lock.
however, looked in msil , saw lock still there.
my next thought framework wasn't acquiring lock because reading data.
i add together write operation within lock , still worked. however, possible don't understand how locking work.
even though works, sense wrong , not convinced not cause issues in production.
i did find question:
use same lock object @ 2 different code block?
which similar believe issue different, i'm asking locking object when calling method has has lock on same object.
obviously code have question works , know how?
am wrong in assuming wrong?
there couple of issues aware of in above code.
public field - know wrong, how in code. circular reference - i'm aware of circular reference , know why bad.thank insight can provide.
you seem under impression class owns lock (aka monitor). that's not case - thread owns monitor.
monitors in .net re-entrant - if thread has monitor, can acquire again. increment "lock count" - when thread releases monitor first time, decrease lock count, count still positive, no other thread able acquire monitor until original thread has released again.
from monitor.enter (the method lock keyword sort-of calls - calls tryenter, but...):
it legal same thread invoke enter more 1 time without blocking; however, equal number of exit calls must invoked before other threads waiting on object unblock.
c# multithreading locking
No comments:
Post a Comment