它是安全阅读并通过不同的线程同时写入到串行端口? [英] Is it safe to read and write to a serial port at the same time via different threads?
问题描述
它是安全的读取和通过不同的线程(一个读线程和一个写线程),同时写入到串行端口?难道有必要添加锁定围绕读/每个线程写?
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?
推荐答案
从的的SerialPort :
任何公共static这种类型的成员(在Visual Basic中的Shared)是线程安全的。所有实例成员都不能保证线程安全的。
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.
这篇关于它是安全阅读并通过不同的线程同时写入到串行端口?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!