• <legend id='ElAAp'><style id='ElAAp'><dir id='ElAAp'><q id='ElAAp'></q></dir></style></legend>
      <bdo id='ElAAp'></bdo><ul id='ElAAp'></ul>
  • <tfoot id='ElAAp'></tfoot>

    <i id='ElAAp'><tr id='ElAAp'><dt id='ElAAp'><q id='ElAAp'><span id='ElAAp'><b id='ElAAp'><form id='ElAAp'><ins id='ElAAp'></ins><ul id='ElAAp'></ul><sub id='ElAAp'></sub></form><legend id='ElAAp'></legend><bdo id='ElAAp'><pre id='ElAAp'><center id='ElAAp'></center></pre></bdo></b><th id='ElAAp'></th></span></q></dt></tr></i><div id='ElAAp'><tfoot id='ElAAp'></tfoot><dl id='ElAAp'><fieldset id='ElAAp'></fieldset></dl></div>

        <small id='ElAAp'></small><noframes id='ElAAp'>

      1. function1 和 function2 是否以线程安全的方式使用我的串口类?

        Does function1 and function2 use my serial port class in thread safe way?(function1 和 function2 是否以线程安全的方式使用我的串口类?)
            <bdo id='cONqa'></bdo><ul id='cONqa'></ul>

                <legend id='cONqa'><style id='cONqa'><dir id='cONqa'><q id='cONqa'></q></dir></style></legend>

                <small id='cONqa'></small><noframes id='cONqa'>

                1. <i id='cONqa'><tr id='cONqa'><dt id='cONqa'><q id='cONqa'><span id='cONqa'><b id='cONqa'><form id='cONqa'><ins id='cONqa'></ins><ul id='cONqa'></ul><sub id='cONqa'></sub></form><legend id='cONqa'></legend><bdo id='cONqa'><pre id='cONqa'><center id='cONqa'></center></pre></bdo></b><th id='cONqa'></th></span></q></dt></tr></i><div id='cONqa'><tfoot id='cONqa'></tfoot><dl id='cONqa'><fieldset id='cONqa'></fieldset></dl></div>
                2. <tfoot id='cONqa'></tfoot>
                    <tbody id='cONqa'></tbody>
                  本文介绍了function1 和 function2 是否以线程安全的方式使用我的串口类?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我有一个围绕串行端口的包装类,看起来像这样:

                  I have a wrapper class around serial port which looks something like this:

                  static class HASPCLass
                  {
                    private static SerialPort m_port;
                    private static bool m_initialized;
                    private static int m_baudRate;
                    static readonly object _syncObject = new object(); 
                    public DoInitialization(int baudRate /*also could be other params*/)
                    {
                      lock(_syncObject)
                      {
                         if (!m_initialized)
                         {
                           Initialize(baudRate);
                         }
                      }
                    }
                  
                    private Initialize(int baudrate /*also could have other params*/)
                    {
                       m_port.open(..);
                       m_baudRate = baudRate;
                       m_initialized = true;
                    }
                  
                    private Uninitialize()
                    {
                       m_port.close();
                       m_initialized = false;
                    }
                  
                  
                  
                    public void Read(byte[] buff) 
                    {
                      lock(_syncObject)
                      {
                        //Other custom read stuff
                        m_port.Read(buff);
                      }
                    }
                  
                    public void Write(byte [] buff) 
                    {
                      lock(_syncObject)
                      {
                        //Other write related code
                        m_port.Write(buff);
                      }
                    }
                  
                    public void Close() 
                    {
                      lock(_syncObject)
                      {
                         if (m_initialized)
                         {
                           Uninitialize();
                         }
                      }
                    }
                  }
                  

                  我尝试让这个类线程安全.有人对其进行了初始化——读取和写入可能从其他线程中使用——最后调用关闭.

                  I tried making this class thread safe. Someone initializes it - read and writes maybe used from other threads - and in the end calls Close.

                  现在想象一下,我有两个额外的静态方法来自其他类,它们执行以下操作:

                  Now Imagine I have two additional static methods from other class which do something like this:

                  public static void function1()
                  {
                   HASPClass.Read(...);
                  
                   // Some other code
                  
                   HASPClass.Write(...);
                  }
                  
                  public static void function2()
                  {
                   HASPClass.Read(...);
                  
                   // Some other code
                  
                   HASPClass.Write(...);
                  }
                  

                  为了整体线程安全,我还将这些函数封闭在锁中:

                  For overall thread safety I also enclosed these functions in locks:

                  public static void function1()
                  {
                     lock(otherlock1)
                     {
                       HASPClass.Read(...);
                  
                       // Some other code
                  
                       HASPClass.Write(...); 
                      }
                  }
                  
                  public static void function2()
                  {
                     lock(otherlock1)
                     {
                        HASPClass.Read(...);
                  
                        // Some other code
                  
                        HASPClass.Write(...); 
                      }
                  }
                  

                  因为调用读取和写入的顺序可能与 HASP 相关.我的问题是:现在我的最终方法(使用 function1 和 function2)是否正确/线程安全?

                  Because order in which read and writes are called might be relavant for the HASP. My question is: is now my final approach (of using function1 and function2) correct/thread safe?

                  推荐答案

                  既然你有点使用单例,那么只要函数不使用必须在 中锁定的资源,你不需要额外的锁就可以了.//其他一些代码.该类本身是线程安全的,因为它使用相同的锁锁定变量的所有使用.这是最紧的.但请确保不要在注释后面的代码中引入死锁.

                  Since you kind of use a singleton you are fine without additional locks as long as the functions do not use resources that have to be locked in // Some other code. The class itself is thread safe because it locks all uses of the variables with the same lock. This is as tight as it gets. But make sure to not introduce dead locks in the code that lies behind the comments.

                  一般来说,您应该确保在所有线程完成之前没有人关闭您的对象.

                  In general you should make sure no one closes your object before all threads are done with it.

                  除了这个代码示例或多或少不一致.你不声明它是静态的,也不写任何返回类型.

                  Besides this code example is more or less inconsistent. You don't declare it static and write no return types and all.

                  从需要以特殊顺序发出命令的更高观点来看,我更正了该语句并说是的,您需要锁定它.

                  From the higher persepctive of the need to give commands in a special order I correct the statement and say yes you need to lock it.

                  但要小心死锁.一种更明确的方式如何出错(尽管我在您的示例代码中没有看到它发生):

                  But beware of dead locks. A more explicit way how this can go wrong (though I don't see it happening in your example code):

                  有 2 个线程可以持有锁.您的设备将始终向您发送 1,除非您向其发送 2,否则它将向您发送 2.

                  There are 2 threads that can hold the lock. Your device will always send you 1 except if you transmit 2 to it then it will send you 2.

                  线程 1 尝试先从设备读取 1,然后再读取 2,而不释放锁.

                  Thread 1 is trying to first read a 1 and after that a 2 from the device without releasing the lock.

                  现在假设以某种方式在接收到 1 后采取的行动启动了想要将 2 传输到设备的线程 2.但它不能因为线程 1 仍在等待,但它会永远等待,因为线程 2 无法传输.最常见的情况是与调用一起使用的 GUI 事件(这会导致其他线程执行代码).

                  Now suppose somehow the actions taken after receiving 1 start Thread 2 which wants to transmit 2 to the device. But it can not because Thread 1 is still waiting but it will wait forever because Thread 2 can not transmit. The most often case for this is GUI events used with invoke (which leads to an other thread executing code).

                  这篇关于function1 和 function2 是否以线程安全的方式使用我的串口类?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  Multicast delegate weird behavior in C#?(C# 中的多播委托奇怪行为?)
                  Parameter count mismatch with Invoke?(参数计数与调用不匹配?)
                  How to store delegates in a List(如何将代表存储在列表中)
                  How delegates work (in the background)?(代表如何工作(在后台)?)
                  C# Asynchronous call without EndInvoke?(没有 EndInvoke 的 C# 异步调用?)
                  Delegate.CreateDelegate() and generics: Error binding to target method(Delegate.CreateDelegate() 和泛型:错误绑定到目标方法)
                  • <bdo id='sEe3m'></bdo><ul id='sEe3m'></ul>

                      <legend id='sEe3m'><style id='sEe3m'><dir id='sEe3m'><q id='sEe3m'></q></dir></style></legend>
                      • <i id='sEe3m'><tr id='sEe3m'><dt id='sEe3m'><q id='sEe3m'><span id='sEe3m'><b id='sEe3m'><form id='sEe3m'><ins id='sEe3m'></ins><ul id='sEe3m'></ul><sub id='sEe3m'></sub></form><legend id='sEe3m'></legend><bdo id='sEe3m'><pre id='sEe3m'><center id='sEe3m'></center></pre></bdo></b><th id='sEe3m'></th></span></q></dt></tr></i><div id='sEe3m'><tfoot id='sEe3m'></tfoot><dl id='sEe3m'><fieldset id='sEe3m'></fieldset></dl></div>

                          <tfoot id='sEe3m'></tfoot>
                              <tbody id='sEe3m'></tbody>
                          • <small id='sEe3m'></small><noframes id='sEe3m'>