简体   繁体   English

在C#中实现多路复用器的硬件锁定

[英]Realize hardware lock for multiplexer in C#

We use a physical (de)multiplexer in our system (time division, one input, several outputs). 我们在系统中使用物理(解)多路复用器(时分,一个输入,多个输出)。

While one channel is routed, an action is executed (eg doing something on the routed connection). 路由一个通道时,将执行一个动作(例如,在路由的连接上执行某些操作)。 During this action, noone else is allowed to route the multiplexer to another channel. 在此操作期间,不允许其他人将多路复用器路由到另一个通道。

For this scenario we currently use the lock statement as in 对于这种情况,我们目前使用如下的lock语句

public void DoSomething(Action action, int channel)
{
    lock(_lock)
    {
        _multiplexer.Route(channel);
        action();
    }
}

Is using lock appropriate in this use case or are there other approaches to handle locking a hardware device? 在这种用例中使用lock是否合适,还是有其他方法来处理锁定硬件设备? I often read 我经常读

Keep locks tight 保持锁紧

and

Never execute arbitrary actions within a lock 切勿在锁内执行任意动作

Do these rules apply in this situation? 这些规则在这种情况下适用吗?

Yes, this is an appropriate use of locks (as opposed to other locking mechanisms). 是的,这是对锁的适当使用(与其他锁定机制相反)。 However, some might argue that another cross-process type lock like a named mutex should be used because if multiple processes could be started, they could both access the hardware at the same time, potentially corrupting data. 但是,有些人可能会争辩说,应该使用另一个跨进程类型锁,例如命名的互斥锁,因为如果可以启动多个进程,则它们都可能同时访问硬件,从而可能破坏数据。

"Keep locks tight" generally means that you should make sure that you don't do any unnecessary processing while holding the lock. “保持锁紧”通常意味着您应确保在握住锁时不要进行任何不必要的处理。 For example, if you need to allocate some space to put data in, that can generally be done outside the lock. 例如,如果您需要分配一些空间来放入数据,则通常可以在锁之外进行。 The second rule is generally to avoid deadlock. 第二条规则通常是避免死锁。 For example, if inside the action passed into DoSomething , the code were to lock a different lock, and in some cases other code were to access that lock before calling DoSomething , if those two code paths happened to run at the same time, you'd have deadlock (one would hold this lock and wait for the other one, and the other would hold the other lock and wait for this one). 例如,如果在传递给DoSomething的操作内部,代码将锁定另一个锁,并且在某些情况下,其他代码将在调用DoSomething之前访问该锁,如果这两个代码路径恰巧同时运行,则您d具有死锁(一个将持有此锁并等待另一个,而另一个将持有另一个锁并等待该锁定)。 If the developers using this code have the discipline to never take any locks or do any unnecessary processing in the specified action, things will work fine, but that's a big if. 如果使用此代码的开发人员有一定的纪律,即从不对任何操作执行任何锁定或执行任何不必要的处理,则一切正常,但是,如果这样做的话,那就大了。

If it's possible, it would be much safer (better) to code up all the possible operations that can be done on the channel in a way that all the locks are hidden within the implementation and no calls to outside code are made while the lock is held. 如果可能的话,以某种方式对通道上可以完成的所有可能的操作进行编码(将所有锁都隐藏在实现中,并且在锁处于锁定状态时不进行对外部代码的调用),将更加安全(更好)。保持。 That way, there is no way someone who doesn't understand the internals could write code that causes deadlock. 这样,任何不了解内部原理的人都无法编写导致死锁的代码。

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

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