我需要在我的程序中使用多个设备,我希望这可以在高级线程API上完成 通常,与设备的通信包括发送命令和接收答案(通过套接字,串行端口等)。
主要问题是与设备的通信无法放置在GUI(主)线程中,因为没有设备立即响应。
现在,我决定与设备on asynchronous operations进行所有通信,例如使用std::async
和std::future
我的主框架(Qt 5)有自己的&# 34;异步" - QtConcurrent::run()
,QFuture
等,但它也没有提供线程选择。下一个问题是我将始终只在一个附加线程中使用所选设备,
因为:
注意:串口总是以独占访问方式打开(也就是说,没有其他进程或线程可以访问已打开的串口)。
我看到了下一个选项:
写我自己的" asyncable"线程类
附加说明:我不想使用仅限事件驱动的逻辑。
我现在想的是什么(简化) 标题:
#include <future>
using namespace std; //
struct TStatus
{
bool health;
};
class AsyncDevice
{
public:
AsyncDevice(IODevice* ioDevice);
future<TStatus> getHealthStatus();
private:
TStatus get_health_impl();
private:
IODevice* m_ioDevice;
EventHandler* m_eventHandler; // Observer to notify
};
CPP:
future<TStatus> AsyncDevice::getHealthStatus()
{
// ** Here I cannot use choose one specified thread ***
future<TStatus> ret = async(launch::async,
&AsyncDevice::get_health_impl, this);
return ret;
}
TStatus AsyncDevice::get_health_impl()
{
writeDeviceCmd();
waitForBytesWritten();
// Here it is easy and simply to wait, read and check an answer
TStatus status;
int timeout = 500;
if (waitForReadyRead(timeout))
{
status = readDeviceAnswer();
}
else
{
status.health = false;
}
m_eventHandler->onHealthStatus(status); // Notify observer
return status;
}
我的主要问题:
对于这类问题,最佳解决方案是什么?你能解释一下吗?
答案 0 :(得分:0)
您正在使用Qt,并且所有使用Qt API的IO都是异步完成的。例如 apiVersion: networking.k8s.io/v1
kind: NetworkPolicy
metadata:
name: default-deny
spec:
podSelector: {}
policyTypes:
- Ingress
立即完成并且没有阻塞。当有数据准备好被读取时,Qt信号QIoDevice::read
被触发,如果你附加一个插槽,你将能够得到你的数据。这意味着您可以在qt上的GUI线程中执行io,并且不需要为此生成新线程。