我一直在寻找一个Qt线程的简洁例子,而我发现的所有内容都是复杂的“眼睛糖果”,声称显示QThreads是如何工作的,但它们太过于难以理解。
我接受了Zen的答案,因为他向我展示了我的尝试所缺失的确切内容,然后添加了我自己的例子,我希望看到它。点击此处跳至:https://stackoverflow.com/a/34561122/3491308
我原来的问题如下:
似乎我必须遗漏一些东西,但我似乎无法让这项工作像我认为的那样。我的完整应用程序需要有两个或三个线程,所有这些线程(几乎)一起启动并永远运行:
核心功能当然是在实时处理线程中,另外两个只是调整它的工作方式。因此,在实时处理器运行时,会根据用户在GUI中的操作来创建,修改和销毁处理对象,并且其中一些对象内的数据也由USB生成和使用。
我一直在寻找如何在Qt中使用线程的示例,但我不断得到复杂的“眼睛糖果”类型的应用程序,这会使线程部分变得混乱。我尽力尝试解释它们并编写我自己的,但我一直在Qt本身得到段错误,我的小“游乐场”项目说我甚至没有得到第二个帖子:
#ifndef MAIN_H
#define MAIN_H
#include <QtWidgets>
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
MainWindow(QWidget* parent = 0);
~MainWindow() {}
private:
QObject* mythingy;
private slots:
void deleteObject(QObject* thingy);
};
class Worker : public QObject
{
Q_OBJECT
public:
Worker(QObject* thingy, QObject* parent = 0);
private:
QObject* mythingy;
signals:
void deleteObject(QObject* thingy);
private slots:
void doWork();
};
#endif // MAIN_H
/***************
*** main.cpp ***
***************/
#include "main.h"
#include <QApplication>
int main(int argc, char* argv[])
{
QApplication a(argc, argv);
MainWindow w;
w.show();
return a.exec();
}
MainWindow::MainWindow(QWidget* parent)
: QMainWindow(parent)
{
mythingy = new QObject(this);
QThread* thisthread = this->thread();
QThread* mainthread = QCoreApplication::instance()->thread();
//breakpoint here to check thisthread and mainthread
Worker* worker = new Worker(mythingy, this);
connect(worker, SIGNAL(deleteObject(QObject*)), this, SLOT(deleteObject(QObject*)));
}
void MainWindow::deleteObject(QObject* thingy)
{
QThread* thisthread = this->thread();
QThread* mainthread = QCoreApplication::instance()->thread();
//breakpoint here to check thisthread and mainthread
delete thingy;
}
Worker::Worker(QObject* thingy, QObject* parent)
: QObject(parent)
{
mythingy = thingy;
QThread* thread = new QThread(this);
this->moveToThread(thread);
//use a timer to allow the constructor to exit
QTimer* timer = new QTimer(this);
timer->setSingleShot(true);
timer->start(1000);
connect(timer, SIGNAL(timeout()), this, SLOT(doWork()));
QThread* thisthread = this->thread();
QThread* mainthread = QCoreApplication::instance()->thread();
//breakpoint here to check thisthread and mainthread
thread->start();
}
void Worker::doWork()
{
QThread* thisthread = this->thread();
QThread* mainthread = QCoreApplication::instance()->thread();
//breakpoint here to check thisthread and mainthread
deleteObject(mythingy);
}
如果有人可以发布一个如何正确执行QThreads的示例,给出我对项目的描述,并尽可能少的杂乱(如果可能的话,最好比我的短),我会非常感激。
根据Zen的回答,这似乎也有效:
MainWindow::MainWindow(QWidget* parent)
: QMainWindow(parent)
{
mythingy = new QObject(this);
QThread* thisthread = this->thread();
QThread* mainthread = QCoreApplication::instance()->thread();
Worker* worker = new Worker(mythingy, this);
}
Worker::Worker(QObject* thingy, QObject* parent)
: QObject(0) //no real parent, so we can move to a different thread
{
mythingy = thingy;
QTimer* timer = new QTimer(this);
timer->setSingleShot(true);
timer->start(1000);
connect(timer, SIGNAL(timeout()), this, SLOT(doWork()));
QThread* thread = new QThread(parent);
this->moveToThread(thread);
thread->start();
}
void Worker::doWork()
{
QThread* thisthread = this->thread();
QThread* mainthread = QCoreApplication::instance()->thread();
delete mythingy;
}
仍然在构造函数中移动自己,现在直接删除对象而不是告诉所有者的线程来执行它。 (请记住,在完整的项目中,所有者已经标记了要删除的对象,但实际上没有这样做,因为另一个线程可能正在使用它)
有什么问题吗?
答案 0 :(得分:1)
1.您无法使用父级移动对象。因此,Worker* worker = new Worker(mythingy, this);
代替Worker* worker = new Worker(mythingy);
2.您无法为处于不同主题的父级创建子级。
MainWindow::MainWindow(QWidget* parent)
: QMainWindow(parent)
{
mythingy = new QObject(this);
QThread* thisthread = this->thread();
QThread* mainthread = QCoreApplication::instance()->thread();
//breakpoint here to check thisthread and mainthread
//*****************
Worker* worker = new Worker(mythingy);
QThread* thread = new QThread();
worker->moveToThread(thread);
thread->start();
//*****************
connect(worker, SIGNAL(deleteObject(QObject*)), this, SLOT(deleteObject(QObject*)));
}
Worker::Worker(QObject* thingy, QObject* parent)
: QObject(parent)
{
mythingy = thingy;
// QThread* thread = new QThread(this);
// this->moveToThread(thread);
//use a timer to allow the constructor to exit
QTimer* timer = new QTimer(this);
timer->setSingleShot(true);
timer->start(1000);
connect(timer, SIGNAL(timeout()), this, SLOT(doWork()));
// QThread* thisthread = this->thread();
// QThread* mainthread = QCoreApplication::instance()->thread();
//breakpoint here to check thisthread and mainthread
// thread->start();
}
对象的插槽总是由它所在的线程执行。因此,由于您从未移动MainWindow
,因此无需在MainWindow::deleteObject
中检查其线程。
例如:
Worker* worker = new Worker(mythingy);
QThread* thread = new QThread();
worker->moveToThread(thread);
thread->start();
//wrong: directly invoking doWork in mainthread
worker->doWork();
//correct: through signal-slot mechanics
connect(this, SIGNAL(startWork()), worker, SLOT(doWork()));
答案 1 :(得分:1)
在Zen的帮助下,这是我正在寻找的简洁例子。如果你一直向下滚动,它很适合(至少在我的屏幕上)。顶部的头文件只是为了使其编译。
#ifndef MAIN_H
#define MAIN_H
#include <QtWidgets>
class Worker : public QObject
{
Q_OBJECT
public:
Worker(QObject* thingy, QObject* parent = 0);
private:
QObject* mythingy;
private slots:
void doWork();
};
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
MainWindow(QWidget* parent = 0);
~MainWindow();
private:
QObject* mythingy;
Worker* myworker;
};
#endif // MAIN_H
/***************
*** main.cpp ***
***************/
#include "main.h"
#include <QApplication>
int main(int argc, char* argv[])
{
QApplication a(argc, argv);
MainWindow w;
w.show();
return a.exec();
}
MainWindow::MainWindow(QWidget* parent)
: QMainWindow(parent)
{
mythingy = new QObject(this);
myworker = new Worker(mythingy, this);
}
MainWindow::~MainWindow()
{
delete myworker;
}
Worker::Worker(QObject* thingy, QObject* parent)
: QObject(0) //no real parent, so we can move to a different thread
{
mythingy = thingy;
//move myself to a new thread and start it
QThread* thread = new QThread(parent);
connect(thread, SIGNAL(started()), this, SLOT(doWork()));
this->moveToThread(thread);
thread->start();
}
void Worker::doWork()
{
//deleting an object from a different thread:
//requires careful planning to make it safe, but seems to work as expected
delete mythingy;
}
评论指出了重要的部分,其余部分再次只是让它完全运行。您可以将其添加到doWork()以验证它是不同的线程:
QThread* thisthread = this->thread();
QThread* mainthread = QCoreApplication::instance()->thread();
在此之后立即设置一个断点,并看到指针是不同的。
至于段错误,基于线程的段落似乎会在我正确的时候消失,只留下天真编写的非线程安全的代码来过早地删除它们。