first cut at making GenericThread use QThread instead of pthread

This commit is contained in:
Brad Hefta-Gaub 2014-01-10 21:16:00 -08:00
parent 7848d065bc
commit b6b2b7f58b
2 changed files with 25 additions and 19 deletions

View file

@ -23,19 +23,30 @@ GenericThread::~GenericThread() {
void GenericThread::initialize(bool isThreaded) {
_isThreaded = isThreaded;
if (_isThreaded) {
pthread_create(&_thread, NULL, GenericThreadEntry, this);
QThread* _thread = new QThread(this);
// when the worker thread is started, call our engine's run..
connect(_thread, SIGNAL(started()), this, SLOT(threadRoutine()));
// when the thread is terminated, add both scriptEngine and thread to the deleteLater queue
//connect(this, SIGNAL(finished()), this, SLOT(deleteLater()));
//connect(_thread, SIGNAL(finished()), _thread, SLOT(deleteLater()));
this->moveToThread(_thread);
// Starts an event loop, and emits _thread->started()
_thread->start();
}
}
void GenericThread::terminate() {
if (_isThreaded) {
_stopThread = true;
pthread_join(_thread, NULL);
_isThreaded = false;
//_isThreaded = false;
}
}
void* GenericThread::threadRoutine() {
void GenericThread::threadRoutine() {
while (!_stopThread) {
// override this function to do whatever your class actually does, return false to exit thread early
@ -51,12 +62,6 @@ void* GenericThread::threadRoutine() {
}
if (_isThreaded) {
pthread_exit(0);
emit finished();
}
return NULL;
}
extern "C" void* GenericThreadEntry(void* arg) {
GenericThread* genericThread = (GenericThread*)arg;
return genericThread->threadRoutine();
}

View file

@ -13,8 +13,7 @@
#include <QtCore/QObject>
#include <QMutex>
#include <pthread.h>
#include <QThread>
/// A basic generic "thread" class. Handles a single thread of control within the application. Can operate in non-threaded
/// mode but caller must regularly call threadRoutine() method.
@ -31,14 +30,18 @@ public:
/// Call to stop the thread
void terminate();
/// If you're running in non-threaded mode, you must call this regularly
void* threadRoutine();
/// Override this function to do whatever your class actually does, return false to exit thread early.
virtual bool process() = 0;
bool isThreaded() const { return _isThreaded; }
public slots:
/// If you're running in non-threaded mode, you must call this regularly
void threadRoutine();
signals:
void finished();
protected:
/// Locks all the resources of the thread.
@ -54,9 +57,7 @@ private:
bool _stopThread;
bool _isThreaded;
pthread_t _thread;
QThread* _thread;
};
extern "C" void* GenericThreadEntry(void* arg);
#endif // __shared__GenericThread__