Solved Killing QThread inside of object. How?
-
Hello all!
I've got an inheritance from previous developer where QThread objects is inside of root object. Something like this:class Class : public QObject { Q_OBJECT public: CustomObject* customObject1 = nullptr; CustomObject* customObject1 = nullptr; QThread* threadHighest; QThread* threadHigh; QThread* threadNormal; QThread* threadLow; QThread* threadLowest;
At the class constructor:
threadHighest = new QThread(this); threadHighest->start(QThread::Priority::HighestPriority); threadHigh = new QThread(this); threadHigh->start(QThread::Priority::HighPriority); threadNormal = new QThread(this); threadNormal->start(QThread::Priority::NormalPriority); threadLow = new QThread(this); threadLow->start(QThread::Priority::LowPriority); threadLowest = new QThread(this); threadLowest->start(QThread::Priority::LowestPriority);
and this
customObject1 = new CustomObject(); cusomObject1->moveToThread(threadLow); customObject2 = new CustomObject(); cusomObject2->moveToThread(threadHigh);
The question is how to kill QThreads correctly at time of killing root object? For now all is working perfectly, but at time of exit of application happening crash.
-
See https://doc.qt.io/qt-5/qthread.html#isInterruptionRequested and https://doc.qt.io/qt-5/qthread.html#wait
Why do you need all this threads?
-
@Christian-Ehrlicher I don't need. It's inheritance.
-
Issues closed. This works for me:
threadLow->quit(); threadLow->wait();
or
threadLow->exit(); threadLow->wait();
or
threadLow->terminate(); threadLow->wait();
-
The question is how to kill QThreads correctly at time of killing root object?
Call
quit()
and thenwait()
for each of the threads. Before you do that signal those custom objects to destroy themselves in their respective threads.. -
@Chris-Kawa I've posted the same few seconds above you :-)))
-
-
@bogong I'm a slow typer I guess :) Good for you.