为什么Mutex实例让控制器正常工作? [英] Why does the Mutex instance let the controller to work properly well?

查看:75
本文介绍了为什么Mutex实例让控制器正常工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是控制器的一部分(我处理ASP.net Web Api 2)。当控制器从客户端收到请求时,它启动并激活Mutex实例。在CATCH块中我实现了互斥实例(RealizeMutex方法),但是在ttru块中我忘了做这个技巧并且它有效,它的表现就像以前被重新启动过一样?为什么?在此先感谢。



名称空间HMMM 
{
公共类MYSTERYController:ApiController
{
public Mutex Mutec = new Mutex();
public JObject MYSTERY([FromBody] string SomeStuff)
{
Mutec.WaitOne(); //关注
尝试
{
// blablabla
//未发布Mutex
返回Error.json(TheSameBlaBlaBla);
}
catch(例外e)
{
Mutec.ReleaseMutex(); //注意
返回Error.json(SomeCoolError);
}
}

}

}

解决方案

< blockquote>这只是一个bug。你需要保证互斥锁总是被释放,所以你不应该在try块下释放它,你不应该在catch块下释放它;你应该只在finally块中释放它。



你是说在没有正确释放互斥锁的情况下它能正常工作吗?可以有两种解释:

  1. Mutex没有做任何有用的事情;换句话说,你实际上没有受互斥锁保护的任何共享资源。
  2. Mutex(更准确地说,不是互斥,但是某种互斥;见下文)是真的需要,但是,只是有机会,您的时间安排永远不会让您到达非专有地访问共享资源的情况。那将是更糟糕的情况。您的代码可以正确工作数月或数年,并在生产过程中在某处崩溃。

    换句话说,您可能已经创建了竞争条件,对时间的不正确依赖性执行,这是一个致命的事情。请仔细阅读并理解: http://en.wikipedia.org/wiki/Race_condition [ ^ ]。



无论如何是,但代码是错误和危险的。你需要使用 finally 阻止吗?这取决于。



首先,你可能不需要互斥锁。互斥锁是否命名?如果没有,你肯定不需要它。如果您没有使用相同共享资源的不同进程,则只有那时您可能需要或可能不需要互斥锁。在其他情况下,您可以使用更轻量级的 lock 语句: https://msdn.microsoft.com/en-us/library/c5kehkcz.aspx [ ^ ]。



此外,你可能没有相互完全排斥。你有几个线程吗?使用ASP.NET,这是不太可能的。你需要了解什么时候不需要,什么时候不需要。只记住:最好的同步是没有同步。请仔细阅读并理解: https://en.wikipedia.org/wiki/Mutual_exclusion [ ^ ]。



< DD> -SA

Here is a part of a controller (I deal with ASP.net Web Api 2 ). When the controller receives a request from the client's side it starts and activates a Mutex instance. In CATCH block I realize the mutex instance (RealizeMutex method) but in the ttru block I have forgot to do this trick and it works, it performs like the have been releazed before? Just why? Thanks in advance.

namespace HMMM
{
    public class MYSTERYController : ApiController
    {
        public Mutex Mutec = new Mutex();
        public JObject MYSTERY([FromBody] string  SomeStuff)
        {
            Mutec.WaitOne();//attention
            try
            {
                //blablabla 
                //not released Mutex                  
                return Error.json("TheSameBlaBlaBla");
            }
            catch (Exception e)
            {
                Mutec.ReleaseMutex();//Attention
                return Error.json("SomeCoolError");
            }
        }

    }

} 

解决方案

This is just a bug. You need to guarantee that the mutex always released, so you should not release it under the "try" block, you should not release it under "catch" block; you should only release it in the "finally" block.

Are you saying that it all "works" properly without correctly releasing the mutex? There can be two explanations:

  1. Mutex does not do anything useful; in other words, you don't really have any shared resources protected by the mutex.
  2. Mutex (more exactly, not mutex, but some kind of mutual exclusion; see below) is really needed, but, just by a chance, your timing never got you to the situation where the shared resource is actually accessed non-exclusively. That would be the worse-case scenario. Your code could work "correctly" for months or years and got crashed somewhere during production.
    In other words, you could have created race condition, incorrect dependency on the time of execution, which is a deadly thing. Please see and understand thoroughly: http://en.wikipedia.org/wiki/Race_condition[^].


Whatever it is, but the code is wrong and dangerous. Do you need to use finally block instead? It depends.

First of all, it's likely that you don't need mutex. Is the mutex named? If not, you most certainly does not need it. If you don't have different processes working with the same shared resource, only then you may or may not need a mutex. In other cases, instead, you can use more lightweight lock statement: https://msdn.microsoft.com/en-us/library/c5kehkcz.aspx[^].

Besides, it could be possible that you don't have mutual exclusion at all. Do you even have several threads? With ASP.NET, this is pretty unlikely. You need to understand when it is not needed and when not. Only remember: "the best synchronization is no synchronization". Please read this and understand thoroughly: https://en.wikipedia.org/wiki/Mutual_exclusion[^].

—SA


这篇关于为什么Mutex实例让控制器正常工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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