简体   繁体   English

没有为其他线程释放锁

[英]Lock not being released for other threads

I have 5 threads which try to enter a critical section of a static class at a random time. 我有5个线程尝试随机进入静态类的关键部分。 If another thread is in the critical section i want the others to 'back-off' and try at a later time. 如果关键部分有其他线程,我希望其他线程“退缩”并稍后再试。 The problem is that it seems that the lock is not being released after the first thread enters the critical section because for the others false will always be returned if i 'breakpoint' at Monitor.TryEnter(thisLock) . 问题在于,在第一个线程进入关键部分后,似乎没有释放该锁,因为对于其他线程,如果我在Monitor.TryEnter(thisLock)处出现“断点”,则总是会返回false。 Any help would be appreciated. 任何帮助,将不胜感激。 Thanks. 谢谢。

This is my code: 这是我的代码:

static class Receiver
    {
        public static object thisLock = new object();
        public static int success;

        public static bool hasLocked()
        {
            if(Monitor.TryEnter(thisLock))
            {
                Monitor.Enter(thisLock);
                System.Threading.Thread.Sleep(10);
                success++;
                Monitor.Exit(thisLock);
                return true;
            }
            return false;
        }
    }

It is legal for the same thread to invoke Enter more than once without it blocking; 同一线程多次调用Enter而不阻塞它是合法的; however, an equal number of Exit calls must be invoked before other threads waiting on the object will unblock. 但是,必须先调用相等数量的Exit调用,然后其他等待该对象的线程才能解除阻塞。

http://msdn.microsoft.com/en-us/library/de0542zz%28v=vs.110%29.aspx http://msdn.microsoft.com/zh-cn/library/de0542zz%28v=vs.110%29.aspx

Basically, you're acquiring the lock two times in your code. 基本上,您在代码中两次获得了锁。 You need to remove the call to Monitor.Enter since Monitor.TryEnter already acquired the lock. 您需要删除对Monitor.Enter的调用,因为Monitor.TryEnter已获取锁定。

static class Receiver
{
    public static object thisLock = new object();
    public static int success;

    public static bool hasLocked()
    {
        if(Monitor.TryEnter(thisLock))
        {
            System.Threading.Thread.Sleep(10);
            success++;
            Monitor.Exit(thisLock);

            return true;
        }

        return false;
    }
}

You're acquiring the locks twice, but only releasing it once. 您获得了两次锁,但是只释放了一次。

If TryEnter succeeds then you will have acquired the lock. 如果TryEnter成功,则您将获得该锁。 This means you don't need to explicitly acquire it again. 这意味着您无需再次明确获取它。 However, you do need to release it explicitly. 但是,您确实需要明确释放它。 So your code should look like this: 因此,您的代码应如下所示:

static class Receiver
    {
        public static object thisLock = new object();
        public static int success;

        public static bool hasLocked()
        {
            if(Monitor.TryEnter(thisLock))
            {
                System.Threading.Thread.Sleep(10);
                success++;
                Monitor.Exit(thisLock);
                return true;
            }
            return false;
        }
    }

Monitors are reenterant, so you can acquire them multiple times. 监视器是可重入的,因此您可以多次获取它们。 However, you must remember to release them by the same number, otherwise they will stay locked. 但是,您必须记住以相同的数字释放它们,否则它们将保持锁定状态。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM