TL;DR:
I was looking for a concise example of Qt threading, and all I found was complex "eye-candy" that claimed to show how QThreads work, but they were simply too much to figure out.
I accepted Zen's answer because he showed me the exact thing that my attempts were missing, then added my own to be the example that I wanted to see. Click here to skip to it: https://stackoverflow.com/a/34561122/3491308
My original question is as follows:
It seems I must be missing something, but I just can't seem to make this work like I think it ought to. My complete application needs to have two or three threads, all of which start (almost) together and run forever:
- The GUI
- Loads a bunch of objects from files before starting the other two threads to process them.
- A realtime processor
- Has TimeCriticalPriority and a periodic QTimer "interrupt" in an attempt to use a PC as a much more powerful embedded system than what I started with. This project started as entirely embedded and quickly became too involved to manage there.
- A USB HID driver
- Connects to the part of the project that is still embedded.
The core functionality, of course, is in the realtime processing thread, and the other two are simply to adjust how it works. So there are processing objects being created, modified, and destroyed while the realtime processor is running, based on the user's actions in the GUI, and the data inside some of those objects is being produced and consumed by USB also.
I keep looking for examples of how to use threads in Qt, but I keep getting complex "eye candy"-type applications that clutter up the threading part. I do my best to try and interpret them and write my own, but I keep getting segfaults in Qt itself and my small "playground" project says that I'm not even getting a second thread:
#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);
}
If someone can post an example of how to do QThreads right, given my description of the project, and with as little clutter as possible (preferably shorter than mine if possible), I'd greatly appreciate it.
Edit:
Based on Zen's answer, this seems to work too:
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;
}
Still moving myself inside the constructor, and now deleting the object directly instead of telling the owner's thread to do it. (remember in the full project that the owner has already marked the object for deletion but refrained from actually doing so because the other thread might be using it)
Anything wrong with that?