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.
这篇关于通过不同的线程同时读取和写入串行端口是否安全?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:通过不同的线程同时读取和写入串行端口是否安全?
基础教程推荐
- 当值可以是对象或空数组时反序列化 JSON 2022-01-01
- 我应该在后面的代码中直接使用 Linq To SQL 还是使 2022-01-01
- .NET SerialPort DataReceived 事件未触发 2022-01-01
- Moq It.Is<>不匹配 2022-01-01
- 如果有人提交恶意软件Nuget包怎么办? 2022-01-01
- 禁止输入少量字符,例如'<'、'&a 2022-01-01
- C# 从 List<List<int>> 中删除重 2022-01-01
- 如何使用 .Net 检查 Active Directory 服务器是否已启动并正在运行? 2022-01-01
- Azure Functions:CosmosDBTrigger 未在 Visual Studio 中触发 2022-01-01
- WPF 模态进度窗口 2022-01-01
