C#中如何检测的对象已经被锁定 [英] C# How to detect an object is already locked

查看:1699
本文介绍了C#中如何检测的对象已经被锁定的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我如何检测一个对象是否被锁定或不?

Monitor.TryEnter (如在的 C#反正来检测,如果一个对象被锁定)对我不起作用,因为它锁定的对象,如果它没有被锁定。

我只要检查它是否在我的code锁定,别的地方我会使用显示器类锁定对象。

我知道这是可能的,例如使用一个布尔字段(例如私人布尔ObjectIsLocked ),但有什么用锁定对象本身检测到它。

这个例子code以下显示了我想要做的:

 私有静态对象myLockObject =新的对象();

私人无效SampleMethod()
{
    如果(myLockObject / *没有锁定* /)//首先检查无锁定它
    {
        ...
        //对象将锁定一些后来在code
        如果收益率(Monitor.TryEnter(myLockObject)!);

        尝试
        {

            ....
        }
        抓住(){...}
        最后
        {
            Monitor.Exit(myLockObject);
        }
    }
}
 

解决方案

你这样做是错的。如果你没有在对象上的锁,你可以不检查是否被锁定(如果你有锁,你会提前知道)。你可以在问被锁定?并获得了不作为回应,然后在接下来的纳秒另一个线程可以采取锁,你的程序将处于损坏状态进入。这根本不是要走的路多线程应用程序的原因所在。NET不具有 Monitor.IsLocked 方法。如果你的code需要检查锁之前获取它让你有一个设计问题。试图与未受保护的标志,以解决这是偶然的,是行不通的100%保证一个贫穷的解决方案。

总之,不要使用布尔 VAR信号多线程的锁定的状态(因为你可以有同样的问题,你看假和1纳秒后,另一线程会写真的话)。使用 Interlock.CompareExchange

 私有静态_lockFlag = 0; // 0  - 免费

如果(Interlocked.CompareExchange(参考_lockFlag,1,0)== 0){
   //只有1个线程将进入这里没有锁定的对象/放
   //其他线程睡觉。

   Monitor.Enter(yourLockObject);

   //释放锁。
   Interlocked.Decrement(REF _lockFlag);
}
 

您会看到,你需要改变_lockFlag上的的每一个地方的,其中一个锁定你的目标可能会获得性。换句话说,你将建立各地土特产一个自定义锁系统。

How can I detect whether an object is locked or not?

Monitor.TryEnter (as described in C# Anyway to detect if an object is locked) does not work for me because it locks the object if it is not locked.

I only want to check if it is locked and somewhere else in my code I will use the Monitor class to lock the object.

I know it is possible to use for example an boolean field (for example private bool ObjectIsLocked) but what to detect it using the lock-object itself.

The example code below shows what I want to do:

private static object myLockObject = new object();

private void SampleMethod()
{
    if(myLockObject /*is not locked*/) // First check without locking it
    {
        ...
        // The object will be locked some later in the code
        if(!Monitor.TryEnter(myLockObject)) return;

        try
        {

            ....
        }
        catch(){...}
        finally
        {
            Monitor.Exit(myLockObject);
        }
    }
}

解决方案

You're doing it wrong. If you don't have the lock on the object you can't check if it is locked (and if you have the lock you will know in advance). You can "ask" "is locked?" and get a "not" as response, then on the next nanosecond another thread can take the lock and your program will enter in a corrupted state. This simply is not the way to go on multithreaded apps and the reason why .NET does not have a Monitor.IsLocked method. If your code needs to check the lock before acquire it so you have a design problem. Trying to solve it with unprotected flags is a poor solution that is guaranteed by 100% of chance that will not work.

Anyway, do not use a bool var to signal multi-thread is locked state (because you can have the same problem, you read "false" and 1 nanosecond later another thread will write "true" to it). Use Interlock.CompareExchange.

private static _lockFlag = 0; // 0 - free

if (Interlocked.CompareExchange(ref _lockFlag, 1, 0) == 0){
   // only 1 thread will enter here without locking the object/put the
   // other threads to sleep.

   Monitor.Enter(yourLockObject); 

   // free the lock.
   Interlocked.Decrement(ref _lockFlag);
}

You'll see that you'll need to change the _lockFlag on every place where a lock to your object could be aquired. In other words, you'll construct a custom lock system around the native one.

这篇关于C#中如何检测的对象已经被锁定的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆