Determining if the current thread has a lock on an object

B

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?
 
W

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]

| 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?
|
|
 
J

Jon Shemitz

Bob said:
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
 
J

Jon Skeet [C# MVP]

William Stacey said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top