Checking whether the current thread owns a lock

后端 未结 10 577
攒了一身酷
攒了一身酷 2021-02-02 13:32

Suppose I have the following code:

public class SomeClass()
{
    private readonly object _lock = new object();

    public void SomeMethodA()
    {
        lock         


        
相关标签:
10条回答
  • 2021-02-02 14:08

    I had similiar requirements of checking whether a lock is locked by the current thread, so I can do something like this.

    public void Method1()
    {
        if(!lockHeld()) lock();
        //DO something
    }
    public void Method2()
    {
        if(!lockHeld()) lock();
        //DO something
    }
    public void Method3()
    {
        if(!lockHeld()) lock();
    
        //Do something
    
        unlock()
    }
    

    So I wrote myself a class for that:

    public class LockObject
    {
        private Object internalLock;
    
        private bool isLocked;
        public bool IsLocked
        {
            get { lock (internalLock) return isLocked; }
            private set { lock (internalLock) isLocked = value; }
        }
    
        public LockObject()
        {
            internalLock = new object();
        }
    
        public void UsingLock(Action method)
        {
            try
            {
                Monitor.Enter(this);
                this.IsLocked = true;
    
                method();
            }
            finally
            {
                this.IsLocked = false;
                Monitor.Exit(this);
            }
        }
    }
    

    Then I can use it as:

        public void Example()
        {
            var obj = new LockObject();
    
            obj.UsingLock(() =>
            {
                //Locked, and obj.IsLocked = true
            });
        }
    

    Note: I ommitted the Lock() Unlock() Methods on this class, but pretty much just wrappers to Monitor.Enter/Exit that set IsLocked to true. The example just illustrates that its very similar to lock(){ } in terms on style.

    Might be unnecessary, but this is useful for me.

    0 讨论(0)
  • 2021-02-02 14:10

    Locking or re-locking are effectively free. The downside of this low cost is that you don't have as many features as the other synchronisation mechanisms. I would simply lock whenever a lock is vital, as you have above.

    If you desparately want to omit 'unnecessary' locks without the risk involved with potential refactoring, add comments. If someone changes your code and it breaks, there is a comment there that explains why. If they still can't figure it out, that's their problem, not yours. A further alternative is to create a class for use as a locking object that contains a boolean you can flick on and off. However, the overheads introduced by doing this (including try/finally blocks, which are not free), are probably not worth it.

    0 讨论(0)
  • 2021-02-02 14:10

    There's a fundamental problem with your request. Suppose there were a IsLockHeld() method and you'd use it like this:

    if (not IsLockHeld(someLockObj)) then DoSomething()
    

    This cannot work by design. Your thread could be pre-empted between the if() statement and the method call. Allowing another thread to run and lock the object. Now DoSomething() will run, even though the lock is held.

    The only way to avoid this is to try to take the lock and see if it worked. Monitor.TryEnter().

    You see the exact same pattern at work in Windows. There is no way to check if a file is locked by another process, other than trying to open the file. For the exact same reason.

    0 讨论(0)
  • 2021-02-02 14:18

    I have no experience whatsoever with .NET programming, but in my own code (Delphi) I have once implemented such assertions by having custom Acquire() and Release() methods in a critical section class, with a private member variable for the id of the thread having acquired the cs. After EnterCriticalSection() the return value of GetCurrentThreadID was written, and before LeaveCriticalSection() the field value was reset.

    But I don't know whether .NET allows a similar thing, or whether you could implement this in your program...

    0 讨论(0)
  • 2021-02-02 14:20

    OK, I now understand your problem. Conceptually I am now thinking of a Semaphore object. You can easily check the semaphore value (a value greater or equal than 0 means the resource is available). Also, this does not actually lock the resource that being done by incrementing or decrementing the semaphore value. The Semaphore object is present in the .NET framework but i did not use it so I cannot provide a proper example. If necessary I can build a simple example and post it here. Let me know.

    Alex.

    L.E. I am unsure now if you actually have control to the synchronization mechanisms in the application. If you only have access to an object that could be locked or not, my solution could prove (once again) of no use.

    0 讨论(0)
  • 2021-02-02 14:22

    I wish that was possible, that would make my code much, much cleaner.

    0 讨论(0)
提交回复
热议问题