Home All Groups Group Topic Archive Search About

Determining if the current thread has a lock on an object

Author
30 May 2006 8:17 PM
Bob
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?

Author
30 May 2006 9:16 PM
William Stacey [MVP]
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.

--
William Stacey [MVP]

Show quote
"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?
|
|
Author
31 May 2006 6:54 AM
Jon Skeet [C# MVP]
William Stacey [MVP] <william.sta***@gmail.com> wrote:
> 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.

This is already available on my OrderedLock class as part of MiscUtil.
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
Author
30 May 2006 9:33 PM
Jon Shemitz
Bob wrote:

> 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.

If you are not using Pulse/Wait, you can try doing a Pulse: The Pulse
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

AddThis Social Bookmark Button