Axtroz
4th October 2012, 13:49
Greetings.
I am trying to understand the concept behind using threads and have subclassed QThread the following way:
// 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;
}
It returns 2 warnings QThread: Destroyed while thread is still running warning
and then it crashes.
However, if I replace
TestThread t1,t2;
with
TestThread *t1 = new TestThread, *t2 = new TestThread;
it works. My question is: why?
I am trying to understand the concept behind using threads and have subclassed QThread the following way:
// 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;
}
It returns 2 warnings QThread: Destroyed while thread is still running warning
and then it crashes.
However, if I replace
TestThread t1,t2;
with
TestThread *t1 = new TestThread, *t2 = new TestThread;
it works. My question is: why?