|
dev
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
Determining if the current thread has a lock on an objectFor debug purposes, I need to figure out if the current thread has a lock on
an object. Ideally I would like to determine this without using multiple threads. The closest I can get is to Monitor.TryEnter(obj)/Exit which tells me whether or not the current thread /can/ acquire a lock on the object, but it does not tell me if it currently /does/ have a lock on the object. I am capable of doing this with some multithreaded trickery but I don't want to do that. I found the sources for JIT_MonTryEnter in clr\src\vm\i386\jithelp.asm and it is doing some interesting stuff in there... which leads me to believe this is not going to be possible. Anybody of a way? You could abstract (wrap existing lock) your own lock with more state (i.e.
thread name) or update a static var or something with current thread name of the thread that owns the lock and clear it after it leaves. -- Show quoteWilliam Stacey [MVP] "Bob" <nob***@nowhere.com> wrote in message news:%23T%23NCXChGHA.1264@TK2MSFTNGP05.phx.gbl... | For debug purposes, I need to figure out if the current thread has a lock on | an object. Ideally I would like to determine this without using multiple | threads. | | The closest I can get is to Monitor.TryEnter(obj)/Exit which tells me | whether or not the current thread /can/ acquire a lock on the object, but it | does not tell me if it currently /does/ have a lock on the object. I am | capable of doing this with some multithreaded trickery but I don't want to | do that. | | I found the sources for JIT_MonTryEnter in clr\src\vm\i386\jithelp.asm and | it is doing some interesting stuff in there... which leads me to believe | this is not going to be possible. | | Anybody of a way? | | William Stacey [MVP] <william.sta***@gmail.com> wrote:
> You could abstract (wrap existing lock) your own lock with more state (i.e. This is already available on my OrderedLock class as part of MiscUtil. > thread name) or update a static var or something with current thread name of > the thread that owns the lock and clear it after it leaves. You can find out which thread owns a lock with the Owner property. See http://www.pobox.com/~skeet/csharp/miscutil -- Jon Skeet - <sk***@pobox.com> http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet If replying to the group, please do not mail me too Bob wrote:
> For debug purposes, I need to figure out if the current thread has a lock on If you are not using Pulse/Wait, you can try doing a Pulse: The Pulse> an object. Ideally I would like to determine this without using multiple > threads. > > The closest I can get is to Monitor.TryEnter(obj)/Exit which tells me > whether or not the current thread /can/ acquire a lock on the object, but it > does not tell me if it currently /does/ have a lock on the object. I am > capable of doing this with some multithreaded trickery but I don't want to > do that. will only succeed if you have the lock, and will raise an exception if you do not. -- ..NET 2.0 for Delphi Programmers <http://www.midnightbeach.com/.net> Delphi skills make .NET easy to learn Being printed - in stores by June |
|||||||||||||||||||||||