Hey all,
I have a quick question regarding the start up of a new thread, separate to the main thread, and executing a slot in it, from a signal invoked from the main GUI thread. I understand this behaviour is not normally recommended.
I created a simple subclass of QThread and connected a signal from my main thread to a slot in the subclassed thread. The slot prints out the current thread id. Initializing my class object in the main thread also prints out the current thread id. However, both these threads have the same id... aren't they supposed to be different? I'm using QThread::currentThread() to return the ID, and I read there might be issues using this with win32, but I'm on Linux. I've provided my source at the bottom of the post.
## output ##
Main thread is 0x90fd250
Executing Slot in 0x90fd250
Executing Slot in 0x90fd250
Executing Slot in 0x90fd250
Main thread is 0x90fd250
Executing Slot in 0x90fd250
Executing Slot in 0x90fd250
Executing Slot in 0x90fd250
To copy to clipboard, switch view to plain text mode
If I move the thread object to the actual thread, (using moveToThread), the results are as expected:
Main thread is 0x8ac5250
Executing Slot in 0x8ace860
Executing Slot in 0x8ace860
Executing Slot in 0x8ace860
Main thread is 0x8ac5250
Executing Slot in 0x8ace860
Executing Slot in 0x8ace860
Executing Slot in 0x8ace860
To copy to clipboard, switch view to plain text mode
I just wanted to know if using moveToThread is the right way to do this (what if the thread quits or is terminated?)
## main.cpp ##
#include <QtCore/QCoreApplication>
#include "test.h"
int main(int argc, char *argv[])
{
MyObject thisIsATest;
return a.exec();
}
#include <QtCore/QCoreApplication>
#include "test.h"
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
MyObject thisIsATest;
return a.exec();
}
To copy to clipboard, switch view to plain text mode
## test.h ##
#include <new>
#include <iostream>
#include <iomanip>
#include <stdlib.h>
#include <vector>
#include <time.h>
#include <math.h>
#include <fstream>
#include <sstream>
#include <stdexcept>
// Qt Includes
#include <QThread>
#include <QMutex>
#include <QString>
#include <QWaitCondition>
class MyObject;
class MyThread;
{
Q_OBJECT
public:
MyThread
(MyObject
* ptr,
QObject *parent
= 0) { MyObjectPtr
= ptr;
} ~MyThread() {}
public slots:
void MyThreadsStartup();
void MyThreadsSlot();
protected:
void run();
private:
MyObject* MyObjectPtr;
};
{
Q_OBJECT
public:
~MyObject() {}
signals:
void TriggerThread();
private:
MyThread* myThread;
};
#include <new>
#include <iostream>
#include <iomanip>
#include <stdlib.h>
#include <vector>
#include <time.h>
#include <math.h>
#include <fstream>
#include <sstream>
#include <stdexcept>
// Qt Includes
#include <QThread>
#include <QMutex>
#include <QString>
#include <QWaitCondition>
class MyObject;
class MyThread;
class MyThread : public QThread
{
Q_OBJECT
public:
MyThread(MyObject* ptr, QObject *parent = 0) { MyObjectPtr = ptr; }
~MyThread() {}
public slots:
void MyThreadsStartup();
void MyThreadsSlot();
protected:
void run();
private:
MyObject* MyObjectPtr;
};
class MyObject : public QObject
{
Q_OBJECT
public:
MyObject(QObject *parent = 0);
~MyObject() {}
signals:
void TriggerThread();
private:
MyThread* myThread;
};
To copy to clipboard, switch view to plain text mode
## test.cpp ##
#include "test.h"
void MyThread::MyThreadsStartup()
{ QObject::connect(MyObjectPtr,
SIGNAL(TriggerThread
()),
this,
SLOT(MyThreadsSlot
()));
//moveToThread(this)
}
void MyThread::MyThreadsSlot()
{ std
::cerr <<
" Executing Slot in " <<
QThread::currentThread() << std
::endl;
}
void MyThread::run()
{ exec(); }
{
std
::cerr <<
" Main thread is " <<
QThread::currentThread() << std
::endl;
myThread = new MyThread(this);
myThread->MyThreadsStartup();
myThread->start();
emit TriggerThread();
emit TriggerThread();
emit TriggerThread();
}
#include "test.h"
void MyThread::MyThreadsStartup()
{ QObject::connect(MyObjectPtr, SIGNAL(TriggerThread()), this, SLOT(MyThreadsSlot()));
//moveToThread(this)
}
void MyThread::MyThreadsSlot()
{ std::cerr << " Executing Slot in " << QThread::currentThread() << std::endl; }
void MyThread::run()
{ exec(); }
MyObject::MyObject(QObject *parent) : QObject(parent)
{
std::cerr << " Main thread is " << QThread::currentThread() << std::endl;
myThread = new MyThread(this);
myThread->MyThreadsStartup();
myThread->start();
emit TriggerThread();
emit TriggerThread();
emit TriggerThread();
}
To copy to clipboard, switch view to plain text mode
Regards,
-KF
Bookmarks