通过不同的线程同时读取和写入串行端口是否安全?

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?

推荐答案

来自 串口:

此类型的任何公共静态(在 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.

这篇关于通过不同的线程同时读取和写入串行端口是否安全?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

DispatcherQueue null when trying to update Ui property in ViewModel(尝试更新ViewModel中的Ui属性时DispatcherQueue为空)
Drawing over all windows on multiple monitors(在多个监视器上绘制所有窗口)
Programmatically show the desktop(以编程方式显示桌面)
c# Generic Setlt;Tgt; implementation to access objects by type(按类型访问对象的C#泛型集实现)
InvalidOperationException When using Context Injection in ASP.Net Core(在ASP.NET核心中使用上下文注入时发生InvalidOperationException)
LINQ many-to-many relationship, how to write a correct WHERE clause?(LINQ多对多关系,如何写一个正确的WHERE子句?)