问题描述
通过不同的线程(一个读线程和一个写线程)同时读写一个串口是否安全?是否有必要在每个线程中添加围绕读/写的锁定?
Is it safe to read and write to a serial port at the same time via different threads (one read thread and one write thread)? Would it be necessary to add locking around reading/writing in each thread?
推荐答案
来自 串口:
此类型的任何公共静态(在 Visual Basic 中为共享)成员都是线程安全的.不保证任何实例成员都是线程安全的.
Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.
由于读取和写入不是静态的,它们不会是线程安全的.无论如何,这是一个非常糟糕的主意,因为 SerialPort 类为您维护内部缓冲区.
Since Read and Write are not static, they would not be threadsafe. It's a very bad idea, in any case, since the SerialPort class maintains internal buffers for you.
您需要将 I/O 与串行端口同步.
You'll need to synchronize your I/O to your serial port.
这篇关于通过不同的线程同时读取和写入串行端口是否安全?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!