本文介绍了请问功能1和功能2使用我的串口类线程安全的方法是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我身边有串口的包装类,它看起来是这样的:

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();
       }
    }
  }
}



我试图使这个类线程安全的。有人对其进行初始化 - 读取和写入也许从其他线程使用 - 并且在最后调用Close

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。
我的问题是:现在是我的正确/线程安全

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.

编辑:从需要给一个特殊订单我更正声明命令越高persepctive并说是的,你需要将其锁定。

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).

这篇关于请问功能1和功能2使用我的串口类线程安全的方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-29 16:51