【发布时间】:2018-03-30 00:40:56
【问题描述】:
我正在研究如何将 Futures 与非阻塞事件驱动代码一起使用(是否在单独的线程中,两者都在)但是我如何从插槽结束未来(〜基于信号解决承诺)?
QByteArray RfidCardReader::startTask(QByteArray send)
{
if(this->busy==false) {
this->sendFrame(send);
QObject::connect(this, &RfidCardReader::frameReady,
[=]() {/*this must be the startTask return*/ return this->int_read_buffer;});
} else {
throw 0;//Handle a queue instead
}
}
QFuture<QByteArray> RfidCardReader::send(QByteArray passed_send)
{
return QtConcurrent::run(QThreadPool::globalInstance(), this->startTask, passed_send);
}
基本上,我只想使用一个实例将串行设备(本质上是同步的)包装在 Futures 队列中,但仅使用非阻塞代码使用 &QIODevice::bytesWritten &QIODevice::readyRead 等信号。 . 如果有更好的解决问题的方法让我知道,我很高兴知道在 Qt 中编写可读异步代码而不阻塞单独线程的正确方法
【问题讨论】:
-
this->startTask格式不正确,您的意思可能是QtConcurrent::run(&RfidCardReader::startTask, this, passed_send)(无论如何都使用QThreadPool::globalInstance()) -
当然,我没有检查代码,因为它显然无法编译,这不是重点,但无论如何感谢,事实上能够在同样的线程也......我正在努力寻找合适的方法来解决这个问题