How to check if I own a monitor

R

Rüdiger Klaehn

Hi all,

is there a way to check if the current thread owns a monitor? I have
an internal method that assumes that it is always called when a
monitor is held by the current thread. Of course I could just lock on
this again, but I want to a) avoid the overhead for this and b)
validate that the caller fulfills the contract.

By the way: is there a separate news group for multithreading
questions?

best regards,

Rüdiger

---

public void PublicMethod()
{
lock(mLock) {
PrivateMethod();
}
}

// this method should only be called with a lock on mLock by the
current thread.
private void PrivateMethod()
{
Debug.Assert( /* the current thread has a lock on mLock */);
// do something short that requires synchronization
}

private readonly object mLock=new object();
 
R

Rüdiger Klaehn

Not per se, AFAIK.


What contract?  The code you posted that deals with the actual lock seems  
to be private within a class.  Isn't it enough to just write the code  
correctly?  
You're a genius. You solved the concurrency crisis. Just write the
code correctly... I should have thought about that. :)
You're not publishing some kind of API, right?
No. The whole point is to have a way to check that I correctly
acquired the monitor. I still think there should be a method for this,
unless it is extremely difficult to implement.
 

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