gpt4 book ai didi

c++ - Qthread 中的运行函数 - 应用程序将挂起

转载 作者:塔克拉玛干 更新时间:2023-11-03 07:24:18 25 4
gpt4 key购买 nike

我在 QT 中使用线程时遇到了一些问题。

#include <QCoreApplication>

#include "handler.hpp"

int main(int argc, char *argv[]) {
QCoreApplication a(argc, argv);

Handler* handler = new Handler();
handler->StartThread();

return a.exec();
}

我期望的是,使用 handler->StartThread() 语句,我的线程中的函数开始编写调试消息,一旦处理程序中的内部计时器完成,我就会得到漂亮的行 [Press ...] 然后返回代码0。然而这没有发生。我得到的是:

I WORK...            ( 0x1540 )
Worker has finished. ( 0x6f4 )
I WORK... ( 0x1540 )
Worker has finished. ( 0x6f4 )
I WORK... ( 0x1540 )
Worker has finished. ( 0x6f4 )
I WORK... ( 0x1540 )
Worker has finished. ( 0x6f4 )
Thread stopped.

当然,当我停止执行应用程序时,返回码是:-1073741510。显然,零不是那么好。无论如何,这是应用程序代码的其余部分:Handler.hpp

#ifndef HANDLER_HPP
#define HANDLER_HPP

#include <QObject>
#include <QThread>
#include <QDebug>
#include <QTimer>

#include "testclass.hpp"

class Handler : public QObject
{
Q_OBJECT
public:
Handler();
~Handler();
void StartThread();

public slots:
void functionFinished();
void threadTerminated();

private:
QTimer* shutdown;
QTimer* timer;
QThread* thread;
MyClass* worker;
};

#endif // HANDLER_HPP

Handler.cpp

#include "handler.hpp"

Handler::Handler() {
shutdown = new QTimer();
thread = new QThread();
timer = new QTimer();
worker = new MyClass();

worker->moveToThread(thread);
QObject::connect(thread, SIGNAL(started()), worker, SLOT(runAgain()));
QObject::connect(timer, SIGNAL(timeout()), worker, SLOT(runAgain()));
QObject::connect(worker, SIGNAL(iFinished()), this, SLOT(functionFinished()));
QObject::connect(shutdown, SIGNAL(timeout()), thread, SLOT(quit()));
QObject::connect(thread, SIGNAL(finished()), this, SLOT(threadTerminated()));

shutdown->start(20000);
}

Handler::~Handler() {
QObject::disconnect(thread, SIGNAL(started()), worker, SLOT(runAgain()));
QObject::disconnect(timer, SIGNAL(timeout()), worker, SLOT(runAgain()));
QObject::disconnect(worker, SIGNAL(iFinished()), this, SLOT(functionFinished()));
QObject::disconnect(shutdown, SIGNAL(timeout()), thread, SLOT(quit()));
QObject::disconnect(thread, SIGNAL(finished()), this, SLOT(threadTerminated()));

if (shutdown != 0) {
delete shutdown;
shutdown = 0;
}

if (timer != 0) {
delete timer;
timer = 0;
}

if (thread != 0) {
delete thread;
thread = 0;
}

if (worker != 0) {
delete worker;
worker = 0;
}
}

void Handler::functionFinished() {
qDebug() << "Worker has finished. (" << QThread::currentThreadId() << ")";
timer->start(5000);
}

void Handler::threadTerminated() {
qDebug() << "Thread stopped.";
}

void Handler::StartThread() {
thread->start();
}

MyClass( header - testclass.hpp)

#ifndef TESTCLASS_HPP
#define TESTCLASS_HPP

#include <QTimer>
#include <QObject>

class MyClass : public QObject
{
Q_OBJECT
public:
MyClass();

public slots:
void runAgain();

signals:
void iFinished();

private:
void doWork();

};

#endif // TESTCLASS_HPP

MyClass 源代码 - testclass.cpp

#include "testclass.hpp"

#include <QThread>
#include <QDebug>

MyClass::MyClass() {

}

void MyClass::runAgain() {
doWork();
}

void MyClass::doWork() {
qDebug() << "I WORK...\t(" << QThread::currentThreadId() << ")";
emit iFinished();
}

我以前读到过,直接从 QThread 继承要在线程内运行的类不是一个好主意,所以我想出了这个解决方案,但它仍然很可疑,尽管它非常好。我愿意接受任何建议,这是我第一次使用 QT,所以现在学习比以后感到遗憾更好。

糟糕,我忘记了具体的实际问题。为什么执行不以良好的退出代码 0 结束?

最佳答案

What I expect is that with handler->StartThread() statement the function within my thread start to write debug messages and once the internal timer within handler finishes I get the nice line [Press ...] and then a return code of 0. However this is not happening.

您没有得到“[Press ...]”命令提示符的原因是 QCoreApplication 及其 exec() 调用。作为Qt docs state :-

Enters the main event loop and waits until exit() is called

因此,您已经创建了第二个线程,将其设置为执行一些工作并完成,但主线程仍在运行。您需要退出主线程。

And of course when I stop the execution of the application, the return code is: -1073741510

听起来您正在用“Ctrl+C”之类的东西杀死主线程。当您的第二个线程完成并清理完毕时调用 QCoreApplication::exit() 应该会有所帮助。

关于c++ - Qthread 中的运行函数 - 应用程序将挂起,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21581220/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com