views:

723

answers:

4

Hi,

Is their anyway to determine if a object is locked in c#. I have the unenviable position, through design where i'm reading from a queue inside a class, and I need to dump the contents into a collection in the class. But that collection is also read/write from an interface outside the class. So obviously their may be a case when the collection is being written to, as the same time i want to write to it.

I could program round it, say using delegate but it would be ugly.

Bob.

+6  A: 

You can always call the static TryEnter class on the Monitor class using a value of 0 for the value to wait. If it is locked, then the call will return false.

However, the problem here is that you need to make sure that the list that you are trying to synchronize access to is being locked on itself in order to synchronize access.

It's generally bad practice to use the object that access is being synchronized as the object to lock on (exposing too much of the internal details of an object).

Remember, the lock could be on anything else, so just calling this on that list is pointless unless you are sure that list is what is being locked on.

casperOne
+3  A: 

Monitor.TryEnter will succeed if the object isn't locked, and will return false if at this very moment, the object is locked. However, note that there's an implicit race here: the instance this method returns, the object may not be locked any more.

Barry Kelly
+1  A: 

Currently you may call Monitor.TryEnter to inspect whether object is locked or not.

In .NET 4.0 CLR team is going to add "Lock inspection API"

Here is a quotation from Rick Byers article:

lock inspection
We're adding some simple APIs to ICorDebug which allow you to explore managed locks (Monitors). For example, if a thread is blocked waiting for a lock, you can find what other thread is currently holding the lock (and if there is a time-out).

So, with this API you will be able to check:
1) What object is holding a lock?
2) Who’s waiting for it?

Hope this helps.

Alex
Interesting.Bob.
scope_creep
A: 

I'm not sure if a static call to TryEnter with a time of 0 will guarantee that the lock will not be acquired if it is available. The solution I did to test in debug mode that the sync variable was locked was using the following:

#if DEBUG
// Make sure we're inside a lock of the SyncRoot by trying to lock it.
// If we're able to lock it, that means that it wasn't locked in the first
// place.  Afterwards, we release the lock if we had obtained it.
bool isNotLocked = false;
try
{
    isNotLocked = Monitor.TryEnter(SyncRoot);
}
finally
{
    if (isNotLocked)
    {
        Monitor.Exit(SyncRoot);
    }
}
Debug.Assert(isNotLocked == false, "The SyncRoot is not locked.");
#endif
Michael Bentley