[英]How to close the programme when running a qtconcurrent from another thread in qt
我正在运行一个具有多线程的程序。 该程序首先有一个主 / UI 线程在其中运行。 在这个程序中,我有一个工人和处理程序类。
工人类有一个模拟函数,它只是生成随机数。 模拟函数连续生成数字而不阻塞任何线程,即通过 Qtconcurrent。
从主/ UI 线程,我已将此工作类放入新线程中。 处理程序类运行在主/UI 线程中,负责通过信号槽与运行在其他线程中的工作类进行通信。
到目前为止一切正常。
当我尝试通过简单地单击应用程序十字按钮来关闭程序时,问题就开始了。 该程序有点挂起它不会关闭。 但是,当我不将 worker 放在另一个类中并从同一个主 /UI 线程运行 worker 类时,就没有问题并且程序以 0 退出。
所以我的问题是如何停止 Qtconcurrent 是另一个线程并最终关闭另一个线程。
谢谢你。
主程序
int main(int argc, char *argv[])
{
QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
QGuiApplication app(argc, argv);
QQmlApplicationEngine engine;
QThread l_newThread;
Worker* l_worker = new Worker();
handler * l_handler = new handler();
l_worker->moveToThread(&l_newThread);
QObject::connect(&l_newThread, &QThread::started, l_worker, &Worker::Init);
QObject::connect(l_handler,&handler::toStop_Signal,&l_newThread, &QThread::quit);
QObject::connect(l_worker, &Worker::toStop_Signal_Worker, l_handler,&handler::toStop_Slot);
QObject::connect(&app,&QCoreApplication::aboutToQuit, l_worker, &Worker::stop);
// QObject::connect(&app,&QCoreApplication::aboutToQuit, &l_newThread, &QThread::quit);
l_newThread.start();
// l_worker->Init();
engine.load(QUrl(QStringLiteral("qrc:/main.qml")));
if (engine.rootObjects().isEmpty())
return -1;
int result = app.exec();
l_newThread.wait();
return result;
}
worker.cpp
#include "worker.h"
Worker::Worker(QObject *parent) : QObject(parent)
{
}
void Worker:: Init()
{
m_simulation = true;
simulate();
}
void Worker::simulate()
{
QtConcurrent::run([this]{
QRandomGenerator generator;
while (m_simulation) {
qint32 t = generator.bounded(0,100);
qDebug() << t;
qDebug() << "sleeping for 1 second";
QThread::sleep(1);
}
if (!m_simulation) {
qDebug() << "Killing the concurrent thread";
// QThread::currentThread()->exit();
emit toStop_Signal_Worker();
}
});
}
void Worker::stop()
{
m_simulation = false;
}
处理程序
#include "handler.h"
handler::handler(QObject *parent) : QObject(parent)
{
}
void handler::toStop_Slot()
{
emit toStop_Signal();
}
结果
QML debugging is enabled. Only use this in a safe environment. 19 sleeping for 1 second 55 sleeping for 1 second 70 sleeping for 1 second 69 sleeping for 1 second Killing the concurrent thread
同时使用底层QThread API和高层QtConcurrent API很奇怪。
但是您可以简单地将QFuture::cancel()
与QFuture::waitForFinished()
来取消任务:
...
QFuture<void> m_future;
...
void Worker::simulate()
{
m_future = QtConcurrent::run([this]{
QRandomGenerator generator;
qint32 t = generator.bounded(0,100);
qDebug() << t;
qDebug() << "sleeping for 1 second";
QThread::sleep(1);
});
}
void Worker::stop()
{
m_future.cancel();
m_future.waitForFinished();
thread()->quit(); // QObject::thread() returns the thread in which the object lives
}
这里可能会发生什么:用于退出l_newThread
的信号toStop_Signal
永远不会被传递,因为当它被发出时,事件循环已经死了。 因此,您的程序卡在等待l_newThread.wait();
的线程l_newThread.wait();
.
我完全不明白你为什么要启动这个线程,只是为了在之后使用QtConcurrent::run
并跨越另一个线程......
无论如何,一旦您确定您的工作人员已停止(根据您发布的输出,您已经停止),您可以直接在main
安全地退出(基本上无用的)线程:
int result = app.exec();
l_newThread.exit(); //just quit it
l_newThread.wait();
return result;
然后你可以摆脱这个连接:
QObject::connect(l_handler,&handler::toStop_Signal,&l_newThread, &QThread::quit);
和(我猜)的处理程序。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.