Greetings.
I am trying to understand the concept behind using threads and have subclassed QThread the following way:
// Header:
#include <QtDebug>
{
public:
void run();
};
// C++ source:
void TestThread::run() {
qDebug() << "Message from thread";
exec();
}
// main.cpp:
#include "testthread.h"
int main(int argc, char** argv) {
TestThread t1, t2;
t1.start();
t2.start();
qDebug() << "Message from main function";
return 0;
}
// Header:
#include <QtDebug>
class TestThread : public QThread
{
public:
void run();
};
// C++ source:
void TestThread::run() {
qDebug() << "Message from thread";
exec();
}
// main.cpp:
#include "testthread.h"
int main(int argc, char** argv) {
TestThread t1, t2;
t1.start();
t2.start();
qDebug() << "Message from main function";
return 0;
}
To copy to clipboard, switch view to plain text mode
It returns 2 warnings QThread: Destroyed while thread is still running warning
and then it crashes.
However, if I replace
TestThread t1,t2;
TestThread t1,t2;
To copy to clipboard, switch view to plain text mode
with
TestThread *t1 = new TestThread, *t2 = new TestThread;
TestThread *t1 = new TestThread, *t2 = new TestThread;
To copy to clipboard, switch view to plain text mode
it works. My question is: why?
Bookmarks